这是我的代码。我想在开放的rootviewController上放一个后退按钮。
- (void)selectSurah:(id)sender {
SurahTableViewController * surahTableViewController = [[SurahTableViewController alloc] initWithNibName:@"SurahTableViewController" bundle:nil];
surahTableViewController.navigationItem.title=@"Surah";
surahTableViewController.navigationItem.backBarButtonItem.title=@"Back";
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:surahTableViewController];
[self presentModalViewController:aNavigationController animated:YES];
}
答案 0 :(得分:8)
我不相信可以将根视图控制器从导航堆栈中弹出,但您可以将UIButton
添加为UIBarButtonItem
的自定义视图来假冒它:
UIButton *b = [[UIButton alloc]initWithButtonType:UIButtonTypeCustom];
[b setImage:[UIImage imageNamed:@"BackImage.png"] forState:UIControlStateNormal];
[b addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
self.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:b];
可以找到合适的iOS UI元素PSD here。
答案 1 :(得分:6)
Faizan,
Helium3 评论很有意义。
我想你的按钮需要以模态方式解除控制器,这是真的吗?如果我错了,请纠正。
如果是这样,您可以创建一个新的UIBarButtonItem
,并设置为UINavigationController
navigationItem
的左(或右)按钮。要不破坏封装,请在viewDidLoad
控制器的SurahTableViewController
方法中创建它。
- (void)viewDidLoad
{
[super viewDidLoad];
// make attention to memory leak if you don't use ARC!!!
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(close:)];
}
-(void)close:(id)sender
{
// to dismiss use dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
// dismissModalViewControllerAnimated: is deprecated
[self dismissViewControllerAnimated:YES completion:^{ NSLog(@"controller dismissed"); }];
}
答案 2 :(得分:4)
由于SurahTableViewController
是导航控制器中的根视图控制器,因此您无法返回根目录,因为您已经在那里。由于您是从其他内容以模态方式呈现的,因此您需要在导航栏上放置一个按钮,其中IBAction
调用:
[self dismissModalViewControllerAnimated:YES];
答案 3 :(得分:3)
UINavigationController中后退按钮的外观和行为依赖于UINavigationControllers堆栈之间的交互。在第一个控制器上放置一个后退按钮违反了这个惯例,没有什么可以回去的,这就是你的代码无法正常工作的原因。
您需要手动将UIBarButtonItem添加到标题栏代码中,如:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
如果您真的希望它看起来像后退按钮,则需要手动创建带有镜像后退按钮的图像的UIBarButtonItem。
另一个建议是,因为看起来你正试图使用后退按钮来关闭一个模态视图控制器,我会坚持使用像“关闭”或“完成”按钮这样更常规的东西来关闭模态视图控制器。后退按钮更适合导航一堆UINavigationControllers。