无法识别的选择器发送到实例

时间:2011-02-26 16:53:24

标签: iphone objective-c

有人知道我为什么会收到这个错误吗?

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CustomRaisedTabViewController cancel:]: unrecognized selector sent to instance 0x4d321e0'

这是失败的代码。这是我的CustomTabViewController。单击“取消”按钮时发生错误。

-(IBAction)showPostModalViewController {

PostActionModalViewController *addController = [[PostActionModalViewController alloc] 
                                                initWithNibName:@"PostActionModalView" bundle:nil];

// Configure the PostAddViewController. In this case, it reports any
// changes to a custom delegate object.

addController.delegate = self;



// Create the navigation controller and present it modally.

UINavigationController *navigationController = [[UINavigationController alloc]
                                                initWithRootViewController:addController];

[self presentModalViewController:navigationController animated:YES];

UIBarButtonItem *cancelButton =
[[UIBarButtonItem alloc] initWithTitle: @"Cancel"
                                 style: UIBarButtonItemStylePlain
                                target: self
                                action: @selector(cancel:)];
addController.navigationItem.rightBarButtonItem = cancelButton;
[cancelButton release];


//[self presentModalViewController:addController animated:true];
[navigationController release];

[addController release];
}

-(IBAction)cancel {
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}

3 个答案:

答案 0 :(得分:9)

因为cancel:方法不是您定义的cancel

将您的cancel操作更改为:

- (IBAction)cancel:(id)sender {
    ...
}

答案 1 :(得分:1)

action: @selector(cancel:) 

对于带参数的动作选择器!取消:这意味着哪个将采用另一个参数。

将您的方法更改为

-(IBAction)cancel:(id)sender{
// Do wat you want
}

or

-(IBAction)cancel:(UIButton *)btnSender{
/// Do what you want
}

答案 2 :(得分:0)

您必须修改

中的取消方法签名
-(IBAction)cancel:(id)sender
 { 
   [self.parentViewController dismissModalViewControllerAnimated:YES]; 
 }

当您将操作添加到cancelButton时(在初始化期间),您指定了“cancel:”选择器,这意味着它将被称为具有一个参数的方法(发送者按钮)