导航栏按钮无法返回上一个视图

时间:2013-10-23 01:44:15

标签: uibutton ios7

我已经按照我在网上找到的信息,我已经设法显示按钮但是当我点击它时,没有任何反应。我是iOS编程的新手,我不确定我是否做错了。

UIImage *backImage = [UIImage imageNamed:@"backIcon.PNG"];
// create the button and assign the image to the button
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);

[backButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *customBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = customBarButton;

和按钮的代码

- (void)goBack:(id)sender {
[self.navigationController popViewControllerAnimated:YES]; }

由于

1 个答案:

答案 0 :(得分:0)

首先,您不必隐藏后退按钮,因为您要添加自己的按钮,第二次尝试使用以下内容替换按钮目标:

[backButton addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];

编辑:

在故事板中使用navigationController的正确方法如下:

1-添加UInavigationController。

2-将rootViewController关系添加到第一个VC1。

3-将push segue从VC1添加到VC2。

通过这种方式,您可以正确地推送和弹出您的VC2。

请考虑阅读this教程,以便更好地了解故事板,细分和导航。

相关问题