【问题标题】:UIAlertView delegate methods not called in iphoneiphone中未调用UIAlertView委托方法
【发布时间】:2012-10-12 12:24:46
【问题描述】:

我正在开发一个应用程序,我有一个要求,即当有来电时调用相应的方法。我编写了alertview代码,它可以完美运行并显示alertview。

Alertview 包含两个按钮接受和拒绝,当我单击这些按钮中的任何一个时,不会调用 alertview 委托方法。

+ (void)incomingCallAlertView:(NSString *)string
{
    UIAlertView *callAlertView=[[UIAlertView alloc] initWithTitle:string message:@""   delegate:self cancelButtonTitle:@"reject" otherButtonTitles:@"accept",nil];
    [callAlertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    NSLog(@"clickedButtonAtIndex");

    if(buttonIndex==0)
    {
          NSLog(@"buttonindex 0");
    }
    else
    {
          NSLog(@"buttonindex 1");
    }

}

我正在使用主线程从另一个方法调用+(void)incomingcall 方法。

- (void)showIncomingcalling
{
     [CallingViewController performSelectorOnMainThread:@selector(incomingCallAlertView:)      withObject:@"on_incoming_call" waitUntilDone:YES];
}

我在课堂上编写协议,即<UIAlertViewDelegate>,但没有调用委托方法,任何人都可以解决我的问题提前谢谢。

【问题讨论】:

    标签: ios iphone objective-c uialertview


    【解决方案1】:
    initWithTitle:string message:@"" delegate:self
                                               ^^
                                          Here it is!
    

    在类方法的上下文中,self 指的是类本身,而不是对象的实例(类方法如何知道类的实例?)。因此,您必须将 incomingCallAlertView: 方法变成 实例方法(即在其前面加上减号而不是加号,然后在 showIncomingCalling 方法中调用类名的 self insetad ),或者像类方法一样实现委托方法:

    + (void)alertView:(UIAlertView *)av clickedButtonAtIndex:(NSInteger)index
    

    (这确实有效,因为类对象本身就是其元类的一个实例,这意味着类方法实际上只是元类的实例方法。)

    等等

    顺便说一句,请仔细阅读一份不错的 Objective-C 教程和/或语言参考。这个问题不应该在这里问,因为它太基础了,无法在其他资源中查找。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多