Objective c 这个代码有什么问题?(目标c)?

Objective c 这个代码有什么问题?(目标c)?,objective-c,uialertview,alertview,Objective C,Uialertview,Alertview,第一个alertmsg工作非常好。但当我在新的“@twitter”按钮上添加like时,它就不起作用了。否则一切正常。我想知道为什么它没有,但它应该……需要帮助。假设 -(void)otherGames { UIAlertView *alertMsg = [[UIAlertView alloc] initWithTitle:@"This gGame was Developed By:" message:@"Burhan uddin Raizada" delegat

第一个alertmsg工作非常好。但当我在新的“@twitter”按钮上添加like时,它就不起作用了。否则一切正常。我想知道为什么它没有,但它应该……需要帮助。

假设

-(void)otherGames
{
    UIAlertView *alertMsg = [[UIAlertView alloc]
    initWithTitle:@"This gGame was Developed By:"
    message:@"Burhan uddin Raizada"
    delegate:nil
    cancelButtonTitle:@"Dismiss"
    otherButtonTitles: @"@twitter" , nil];
    [alertMsg show];

}

-(void)alertMsg:(UIAlertView *)alertMsg clickedButtonAtIndex:(NSInteger)buttonIn… {

    if (buttonIndex == 1) {
        NSString *containingURL = [[NSString alloc] initWithFormat:@"http://www.twitter.com/…
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString: containingURL]];
    }
}

您必须将代理设置为
self
,并将delegat协议添加到标题中:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
@界面yourView:UIViewController
编辑:根据@holex,使用
alertView:willDismissWithButtonIndex:
而不是-
(void)alertView:(UIAlertView*)alertView单击按钮Index:(NSInteger)buttonIndex

更新 由于在iOS8中不推荐使用
UIAlertView
,此答案已过时

你可以在苹果的开发文档上阅读更多信息


第一名:

您尚未委派您的类,
delegate:nil
显示
UIAlertView
没有委派类。您应该按照以下步骤纠正您的方法:

@interface yourView : UIViewController <UIAlertViewDelegate>
秒:

回调方法的正确名称为:
-alertView:didDismissWithButtonIndex:

-(void)otherGames
{
    UIAlertView *alertMsg = [[UIAlertView alloc]
    initWithTitle:@"This gGame was Developed By:"
    message:@"Burhan uddin Raizada"
    // delegate:nil
    delegate:self // don't forget to implement the UIAlertViewDelegate protocol in your class header
    cancelButtonTitle:@"Dismiss"
    otherButtonTitles: @"@twitter" , nil];
    [alertMsg show];

}

现在,您知道了代码片段错误的原因。

无需将您的问题标记为紧急问题。在这样一个大的社区中,每一个问题都会得到应有的关注。首先:您没有委派您的类,
delegate:nil
显示
UIAlertView
没有委派类。第二:回调方法的正确名称是:
-alertView:didDismissWithButtonIndex:
。。。现在,您知道为什么这个代码片段是完全错误的了。还有其他问题吗?:)@holex,你为什么不提供一个答案?@vikingosegundo,我的答案已经在上面提供了,我想他应该能编出正确的代码了。@holex但是这个答案不能被接受,因此这个线程将显示为未回答。这会导致其他人感到困惑,系统会随机触摸它以引起注意。因为问题不应该指向答案,所以评论应该包含评论,而不是答案。我想你知道这会导致什么样的结果,这些都属于答案。这仍然是错误的。回调方法的名称肯定不是这样。你是对的,这就是为什么我认为他写错了方法。但是如果方法编写正确,它可能无法工作,因为他没有将代理设置为
self
-1。两次你怎么能对这个错误的方法说呢?
-alertView:ClickedButtonIndex:
与好方法
-alertView:didDismissWithButtonIndex:
。。。?您是否阅读过《UIAlertViewDelegate协议参考》protocol reference?在让我-1之前,请尝试阅读我所写的内容:我假设他错误地编写了方法,而不是他使用了错误的方法。-1。第三次,因为你的方法仍然是错误的,似乎你没有注意到它。你想什么时候纠正你回答中的错误方法(
-(void)otherGames
{
    UIAlertView *alertMsg = [[UIAlertView alloc]
    initWithTitle:@"This gGame was Developed By:"
    message:@"Burhan uddin Raizada"
    // delegate:nil
    delegate:self // don't forget to implement the UIAlertViewDelegate protocol in your class header
    cancelButtonTitle:@"Dismiss"
    otherButtonTitles: @"@twitter" , nil];
    [alertMsg show];

}
//-(void)alertMsg:(UIAlertView *)alertMsg clickedButtonAtIndex:(NSInteger)buttonIn… { // WRONG, where have you got this silly idea...?
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        NSString *containingURL = [[NSString alloc] initWithFormat:@"http://www.twitter.com/…
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString: containingURL]];
    }
}