【问题标题】:UIAlertView delegate method crashingUIAlertView 委托方法崩溃
【发布时间】:2012-06-06 11:11:27
【问题描述】:

在我的 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。当点击 Ok 按钮时,我想导航到 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];
    }
}

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

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

【问题讨论】:

    标签: iphone delegates uialertview presentmodalviewcontroller


    【解决方案1】:

    换个方法就行了

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

    【讨论】:

    • 嗨,我仍然有这个问题。当我从任何其他 AlertViews 调用委托方法时,它工作正常。但是,当我单击来自上述方法的警报视图上的确定按钮时,它不起作用。
    • 在委托方法条件中检查 UIAlertView 名称并相应地执行操作。喜欢if(alertview == your_alertview_name) 然后执行一些操作等等
    • @Mithun:当您单击警报视图上的“确定”按钮时遇到什么问题?
    • 我正在从 A 类调用 - (void)hideAlert:(NSString*)message,并显示警报视图。单击确定按钮时,我想导航到另一个视图。我正在获取警报视图,但是当单击“确定”按钮时,出现错误 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFType alertView:clickedButtonAtIndex:]: unrecognized selector sent to instance 0x81baa80” .
    • 但我在 B 类中的 UIButton 操作上创建了另一个警报视图,该操作位于 B 类本身中,当我单击“确定”按钮时,它工作正常。所以问题只在于上述方法的警报视图。
    【解决方案2】:

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

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

    如果你查看UIAlertViewDelegate 类参考文档

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

    【讨论】:

    • 我在代码中添加了 - (void)alertViewCancel:(UIAlertView *)alertView,但是当我点击按钮时,这个方法没有被调用。
    • 所以你的意思是,我说你使用的“取消按钮”只是标题为“OK”。对吗?
    • 是的,你是对的。它是取消按钮,它的名字是“OK”。
    • 我不知道为什么你的- (void)alertViewCancel:(UIAlertView *)alertView Cancel 没有被调用。实际上它应该被调用。但是..嗯。顺便说一句,Maulik 的方法可以暂时解决问题并进一步实现您的目标
    • 到底是谁对答案投了反对票?我真的很同情他。至少他本可以评论否决 X-(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多