在 xcode 5 故事板中返回上一个 ViewController

Posted

技术标签:

【中文标题】在 xcode 5 故事板中返回上一个 ViewController【英文标题】:Go back to previous ViewController in xcode 5 storyboard 【发布时间】:2014-01-10 12:10:30 【问题描述】:

我正在尝试在我的 iPhone 应用程序上创建一个后退按钮,我想返回我的登陆视图。我正在使用此代码,但它不起作用

UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
[back setFrame:CGRectMake(0, 0, 24, 24)];
[back setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[back addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithCustomView:back];
self.navigationItem.hidesBackButton= YES;
navEng.leftBarButtonItem = btnBack; 


-(void)goback [self.navigationController popToRootViewControllerAnimated:YES]; 

【问题讨论】:

【参考方案1】:

试试这个....刚刚更改了最后一行代码

UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom]; [back setFrame:CGRectMake(0, 0, 24, 24)]; [back setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[back addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithCustomView:back];
self.navigationItem.hidesBackButton= YES;
self.navigationItem.leftBarButtonItem = btnBack;

-(void)goback

     [self.navigationController popToRootViewControllerAnimated:YES]; 

【讨论】:

【参考方案2】:

navEng 在头文件中声明为 IBoutlet UINavigationItem 并出现后退按钮并且操作正确处理,但导航到前一个 ViewController 不起作用

@property (nonatomic, strong) IBOutlet UINavigationItem *navEng;
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setFrame:CGRectMake(0, 0, 24, 24)];
    [back setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.hidesBackButton= YES;
    navEng.leftBarButtonItem = btnBack;



  -(void)goback
       NSLog(@"back");
       [self.navigationController popViewControllerAnimated:YES];
    

【讨论】:

以上是关于在 xcode 5 故事板中返回上一个 ViewController的主要内容,如果未能解决你的问题,请参考以下文章

什么是 Xcode 6 故事板中的“限制到边距”

水平对齐在 Xcode 故事板中不被视为 X 位置约束吗?

如何在 xcode 6.3 界面构建器故事板中默认模拟指标?

如何在 xcode 故事板中以横向模式显示场景?

Xcode:如何在 IOS5 故事板中添加新的标签屏幕?

如何在 Xcode 11 故事板中查看祖先和后代的约束?