2

在我的 iPhone 应用程序中,我有一个NSObjectA 类和一个UIViewControllerB 类。我想从 A 调用 B 类中的一个实例方法。我使用了以下代码。

Bclass *vc = [[Bclass alloc]init];
[vc hideAlert:NSString];
[vc release];

在 B 类中:

- (void)hideAlert:(NSString*)message{
    UIAlertView *shareAlrt = [[UIAlertView alloc] initWithTitle:@""
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [shareAlrt show];
    [shareAlrt release];
}

并调用该方法并显示一个 AlertView。单击确定按钮时,我想导航到 Cclass 类。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        Cclass *vc = [[Cclass alloc]initWithNibName:@"Cclass" bundle:[NSBundle mainBundle]];
        [self presentModalViewController:vc animated:NO];
        [vc release];
    }
}

但是当我点击确定按钮时,应用程序崩溃了。这里发生了什么事?我已经<UIAlertViewDelegate>在 B class.h 文件中添加了,但仍然是同样的错误。请帮忙

我收到错误代码*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType alertView:clickedButtonAtIndex:]: unrecognized selector sent to instance 0x81baa80'

4

2 回答 2

0

换个方法就好

- (void)hideAlert:(NSString*)message{
    UIAlertView *shareAlrt = [[UIAlertView alloc] initWithTitle:@""
                                                    message:message
                                                   delegate:self
                                          cancelButtonTitle:nil
                                          otherButtonTitles:@"Ok",nil];
    [shareAlrt show];
    [shareAlrt release];
}
于 2012-06-06T11:40:07.317 回答
-1

假设您没有其他按钮,除了标题为“确定”的取消按钮,这已经得到了回答。通过查看您显示的代码进行假设。

您使用了取消按钮,您无法在该按钮上处理委托来执行任何操作。

如果您查看UIAlertViewDelegate 类参考的文档

或者,您可以实现 alertViewCancel: 方法以在系统取消您的警报视图时采取适当的操作。如果委托没有实现这个方法,默认的行为是模拟用户点击取消按钮并关闭视图。

于 2012-06-06T11:18:34.013 回答