2012-03-26 70 views
2

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

//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 

} 我做了所有上面的代码,但仍不能采取任何行动。无论我点击哪个按钮,都会使警报消失。这段代码有什么问题,任何人都可以帮忙? ANSWER

亲爱PeterG.评论更改delegete:nildelegate:self和现在的工作。

+3

委托:无将无法正常工作...更改为委托:自我 – 2012-03-26 14:00:40

+0

我曾经尝试过。但它给出了一个错误 – ilhnctn 2012-03-26 14:01:28

+0

@PeterG。对不起兄弟,它工作 – ilhnctn 2012-03-26 14:02:45

回答

2

代表应该可以设置为self。如果它在这里发布错误。

另外我不认为你应该autorelease警报伊娃。尝试跳过该行,看看会发生什么?

+0

啊,你已经得到它的工作:P – Sunkas 2012-03-29 07:52:18

+0

在编辑我提到过。多谢兄弟:) – ilhnctn 2012-03-29 07:59:57

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]; 
}