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

时间:2012-08-07 13:48:46

标签: objective-c uialertview alertview

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

第一个alertmsg工作正常。但是当我在新的“@twitter”按钮中添加了类似的东西时,它就不起作用了。否则一切都很好。我想知道它为什么没有,但它应该......需要帮助。

2 个答案:

答案 0 :(得分:-1)

假设

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

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

您必须将委托设置为self并将委托协议添加到标题中:

@interface yourView : UIViewController <UIAlertViewDelegate>

编辑:根据@holex,使用alertView:willDismissWithButtonIndex:代替 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

答案 1 :(得分:-2)

更新

此答案已过时,因为iOS8中已弃用UIAlertView

您可以在Apple的Dev Docs上阅读有关UIAlertController的更多信息。


<击> 的 FIRST:

<击>

您尚未委托自己的班级,delegate:nil表示UIAlertView没有委托班级。你应该在这之后纠正你的方法:

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

}

<强> SECOND:

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

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

现在,你知道为什么你的代码片段是错误的。