在 IOS 中关闭 ViewController 的按钮处理程序语法

Button Handler Syntax for Dismissing ViewController in IOS

我已经在代码中创建了一个自定义 UIBarButton 来取消自定义 viewController。但是,编译器不喜欢我的语法。

下面是创建 UIBarButtonItem 的代码:

//grab VC
detailC* detailVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"detailVC"]; //This custom VC
is a sub-class of UIViewController

//Create barbuttonitem
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]
                                    initWithImage:[UIImage imageNamed:@"close.png]
                                    style:UIBarButtonItemStylePlain
                                    target:self
                                   action:@selector(dismss:detailVC)];//ERROR HERE

//Here is the dismiss method: 
-(void) dismissVC: (UIViewController*) vc {

  [vc dismissViewControllerAnimated:YES completion:nil];
}

编译器的错误是:'Expected :'

insert : 有一个修复选项。如果我选择修复,它会将选择器更改为:

action:@selector(dismissVC: controller:)];

这没有意义,还给出了警告 'Undeclared Selector"

我做错了什么?

注意:这是在 appDelegate 中进行的,因此我无法使用 self 关闭 VC。

您的尝试从根本上来说是不正确的。您的 dismissVC: 方法可以采用的唯一有效参数是触发操作的 UIBarButtonItem。您不能将视图控制器传递给该方法。

但你不需要。由于您想关闭 "self",只需在 self 上调用 dismissViewController

//Create barbuttonitem
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]
                                initWithImage:[UIImage imageNamed:@"close.png"]
                                style:UIBarButtonItemStylePlain
                                target:self
                                action:@selector(dismiss:)];

这是更新后的方法:

- (void)dismiss:(UIBarButtonItem *)button {
    [self dismissViewControllerAnimated:YES completion:nil];
}