Что не так с этим кодом? (Цель-с)?

-(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 ответа

При условии, что

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

ОБНОВИТЬ

этот ответ устарел с UIAlertView устарела в iOS8.

Вы можете прочитать больше о UIAlertController на Apple Dev Docs.


ПЕРВЫЙ:

Вы не делегировали свой класс, 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];

}

ВТОРОЙ:

правильное имя метода обратного вызова: -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]];
    }
}

Теперь вы знаете, почему ваш фрагмент кода неверен.

Другие вопросы по тегам