In my ViewController I have implemented the UIAlertViewDelegate and in my Model I have a method which creates an UIAlertView with the delegate set to self.delegate which is an instance of the ViewController. Then I try to call willPresentAlertView like this (in Model):
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid operation"
message:@"Cannot divide by 0!"
delegate:self.delegate
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Undo division",nil];
[self.delegate willPresentAlertView:alert];
The willPresentAlertView is implemented in ViewController like this:
- (void)willPresentAlertView:(UIAlertView *)alertView
{
[alertView show];
}
I then get an error Thread 1:EXC_BAD_ACCESS (code=2,adress=0xbf7fff6c) when calling [alert show]
.
If I set delegate to nil
or self it works fine, but I want to call alertView:alertView clickedButtonAtIndex:buttonIndex
when I press a button on the alert, which doesn't work with delegate set to nil or self. Only the ViewController implements these methods, why it should be self.delegate, or?
I want the model to invoke the alert, but I want the ViewController to handle the alert methods such as alertView:alertView clickedButtonAtIndex:buttonIndex
.
How can this be done?