简体   繁体   English

取消按钮未显示在iPad模式下的UINavigationController中

[英]Cancel button not showing up in UINavigationController in modal for iPad

I'm trying to add a cancel button for the modal in the navigation bar for iPad. 我正在尝试为iPad的导航栏中的模式添加一个“取消”按钮。

I'm using the following code: 我正在使用以下代码:

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:abPersonController];
    navController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                          target:self
                                                                                          action:@selector(dismissModal)];
    [self presentViewController:navController animated:YES completion:nil];

For some reason though, the cancel button does not show up. 但是由于某种原因,取消按钮不会显示。

Using spark inspector, I'm seeing a back indicator where the cancel button is supposed to be but it is hidden, and when you run it in on iPad Simulator, you cannot see the cancel button or the back indicator. 使用Spark Inspector,我看到应该有取消按钮但隐藏的后退指示器,并且在iPad Simulator中运行它时,看不到取消按钮或后退指示器。

I tried setting the back indicator's hidden property to NO but nothing shows up. 我尝试将后指示器的hidden属性设置为NO,但没有任何显示。

Edit: The only way I can get it to work is if I add the button in the completion block: 编辑:我可以使它起作用的唯一方法是,如果我在完成块中添加按钮:

[self presentViewController:navController animated:YES completion:^{abPersonController.navigationItem.leftBarButtonItem = doneButton;}];

But this solution looks bad because the done button pops in after the modal has already been on screen. 但是该解决方案看起来很糟糕,因为在模态已经显示在屏幕上之后,弹出完成按钮。 Are there any other ways to get it so it doesn't have to be in the completion block? 是否有其他方法可以获取它,所以它不必在完成块中?

Edit: 编辑:

        UIViewController *dummyView = [[UIViewController alloc] init];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:dummyView];
        [navController pushViewController:abPersonController animated:NO];
        abPersonController.navigationItem.hidesBackButton = YES;
        abPersonController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                              target:self
                                                                                              action:@selector(dismissModal)];
        navController.modalPresentationStyle = UIModalPresentationFormSheet;
        navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:navController animated:YES completion:nil];
        navController.view.superview.bounds = CGRectMake(0, 0, 320, 480);

Should be backBarButtomItem instead of leftBarButtomItem. 应该是backBarButtomItem而不是leftBarButtomItem。

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:abPersonController];
navController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                      target:self
                                                                                      action:@selector(dismissModal)];
[self presentViewController:navController animated:YES completion:nil];

How about: 怎么样:

1. 1。

navController.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                                      target:self
                                                                                                      action:@selector(dismissModal)];

2. 2。

abPersonController.navigationItem.lefBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                                   target:self
                                                                                                   action:@selector(dismissModal)];

Or... 要么...

In abPersonController object's class -viewDidLoad (or init ) you can do: abPersonController对象的类-viewDidLoad (或init )中,您可以执行以下操作:

[self.navigationItem setLeftBarButton:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                    target:self
                                                                                    action:@selector(dismissModal)]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM