Whats wrong with this code? (objective-c)?

203 Views Asked by At
-(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]];
    }
}

the first alertmsg is working absolutely fine. but when i added a like to the new "@twitter" button, then it just doesn't work. otherwise everything is working fine. i am wondering why it doesn't, but it should..need help.

2

There are 2 best solutions below

7
On

Assuming that

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

is

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

You must set your delegate to self and add the delegat protocol to your header :

@interface yourView : UIViewController <UIAlertViewDelegate>

Edit : According to @holex, use alertView:willDismissWithButtonIndex: instead of -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

0
On

UPDATE

this answer has been outdated since UIAlertView is deprecated in iOS8.

you can read more about UIAlertController on Apple's Dev Docs.


FIRST:

you haven't delegated your class, delegate:nil shows there is no delegated class for the UIAlertView. your should correct your method following this:

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

the correct name of the callback method is: -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]];
    }
}

now, you know why your code fragment is wrong.