ilhnctn
ilhnctn

Reputation: 2210

UIAlertView delegate methods don't response on iOS 5.1

I created an instance of UIAlertView with two buttons and in the interface file of my class(.h) I set the delegate too but still cant get any response when clicking the buttons. Here is my code:

 //myClass.h
    @interface MainMenu : UIViewController<UIAlertViewDelegate>
-(IBAction)secondAct:(id)sender;

And the implementation

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

and the delegate method:

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

} I did all of the code above but still can't take any action. Whichever of the buttons i click, it dissmisses the alert. What is wrong with this code can any one help? ANSWER

Dear PeterG. commented to change delegete:nil with delegate:self and it works now.

Upvotes: 2

Views: 1400

Answers (2)

Ravikumar
Ravikumar

Reputation: 85

Just give the delegate "self".

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

Upvotes: 0

Sunkas
Sunkas

Reputation: 9590

Delegate should probably be set to self. If it generates an error post it here.

Also I do not think you should autorelease the alert ivar. Try to skip that line and see what happens?

Upvotes: 2

Related Questions