2

我创建了一个带有两个按钮的 UIAlertView 实例,并在我的类(.h)的接口文件中我也设置了委托,但在单击按钮时仍然无法得到任何响应。这是我的代码:

 //myClass.h
    @interface MainMenu : UIViewController<UIAlertViewDelegate>
-(IBAction)secondAct:(id)sender;

以及实施

-(IBAction)secondAct:(id)sender
      alert = [[UIAlertView alloc] initWithTitle:@"Dear User"
                                                        message:@"Your Request Will be Sent To Security"
                                                       delegate:nil
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"OK", nil];
  [alert show];
  [alert autorelease];
}

和委托方法:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
 NSLog(@"UIAlertView delegate works");//this line too isnt displayed
 NSString *title=[ alertView buttonTitleAtIndex:buttonIndex ];
 if ([title isEqualToString:@"OK"]) {
NSLog(@"OK Pressed");
}//i want to create something like this

我完成了上面的所有代码,但仍然无法采取任何行动。无论我单击哪个按钮,它都会解除警报。这段代码有什么问题,任何人都可以帮忙吗? 回答

亲爱的彼得 G。评论更改delegete:nildelegate:self现在可以使用。

4

2 回答 2

2

委托可能应该设置为自我。如果它产生错误,请在此处发布。

另外,我认为您不应该自动释放警报 ivar。尝试跳过该行,看看会发生什么?

于 2012-03-29T07:50:19.843 回答
0

只需给代表“自我”。

-(IBAction)secondAct:(id)sender
      alert = [[UIAlertView alloc] initWithTitle:@"Dear User"
                                                        message:@"Your Request Will be Sent To Security"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"OK", nil];
  [alert show];
}
于 2014-09-08T09:32:38.443 回答