简体   繁体   English

“取消”按钮 (UIBarButtonItem) 在 iOS 视图 controller 中不起作用

[英]"Cancel" button (UIBarButtonItem) not working in iOS view controller

I am trying to add a "Cancel" button in my viewcontroller's navigation bar programmatically.我正在尝试以编程方式在我的视图控制器的导航栏中添加一个“取消”按钮。 The sole purpose of the button is to dismiss the current viewcontroller and go back to the root viewcontroller.该按钮的唯一目的是将当前视图控制器和 go 解散回根视图控制器。 I am using the following Objective-C code to achieve this:我正在使用以下 Objective-C 代码来实现此目的:

UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onTapCancel:)];
self.controller.navigationItem.leftBarButtonItem = cancelBtn;
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.controller];
[self.navigationController setNavigationBarHidden:NO animated:YES];

Cancel button function:取消按钮 function:

-(void)onTapCancel:(UIBarButtonItem*)item{
    NSLog( @"Cancel button Tapped");
    UIViewController *rootViewController = [
      [[UIApplication sharedApplication] keyWindow] rootViewController];
    [rootViewController dismissViewControllerAnimated:YES completion:nil];
}

The onTapCancel function gets invoked without any issues when I just have a log line instead of the code to dismiss the current viewcontroller.当我只有一个日志行而不是关闭当前视图控制器的代码时,onTapCancel function 被调用而没有任何问题。 But when the code to dismiss the viewcontroller is added (lines 2-4 in the onTapCancel function), the function stops getting invoked (I don't even see the "Cancel button Tapped" log line in the logs).但是当添加关闭视图控制器的代码时(onTapCancel 函数中的第 2-4 行),function 停止被调用(我什至没有在日志中看到“Cancel button Tapped”日志行)。 What could be the possible reason for this?这可能是什么原因?

Following code works (log line is printed everytime the cancel button is tapped) when used in onTapCancel function:以下代码在 onTapCancel function 中使用时有效(每次点击取消按钮时都会打印日志行):

-(void)onTapCancel:(UIBarButtonItem*)item{
    NSLog( @"Cancel button Tapped");
}

Thanks!谢谢!

After a closer look at your code I can see now that you try to dismiss a root controller of your application.仔细查看您的代码后,我现在可以看到您尝试关闭应用程序的根 controller。 It doesn't make any sense on it's own, because the root controller is not dismissible, you can only replace it.它本身没有任何意义,因为根 controller 不可关闭,您只能替换它。 If the controller you are trying to dismiss is in a navigation controller stack, then popping to the root view controller should look like this:如果您尝试关闭的 controller 在导航 controller 堆栈中,则弹出到根视图 controller 应该如下所示:

- (void)onTapCancel:(UIBarButtonItem*)item {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

If you present the controller modally (and from the first snippet of code, I assume the controller is also wrapped with a navigation controller), then just dismiss it:如果您以模态方式展示 controller(从第一段代码开始,我假设 controller 也包含导航控制器),然后将其关闭:

- (void)onTapCancel:(UIBarButtonItem*)item {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

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

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