在 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的主要内容,如果未能解决你的问题,请参考以下文章