Do I need to add the <UIAlertViewDelegate> protocol?
I added an alertView to display an alert message to the user (see below)
-(void)connectionAlert {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
message:@"<Alert message>"
delegate:self
cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
What I then call from my viewController using [self connectionAlert];
, everything works fine, but I'm not sure if I should or shouldn't add a protocol <UIAlertViewDelegate>
to my viewController interface.
Currently I have NOT added the protocol and everything seems to work, is it because I am calling the UIAlertView through myself? Do I really have to add the protocol anyway?
many thanks
Gary
a source to share
I've never had to add the UIAlertViewDelegate protocol, so I don't understand why you need it.
You only need a protocol if you want to access the following methods:
Response to action
– alertView:clickedButtonAtIndex:
Custom setting
– willPresentAlertView:
– didPresentAlertView:
– alertView:willDismissWithButtonIndex:
– alertView:didDismissWithButtonIndex:
Cancellation
– alertViewCancel:
If you don't want to listen to these notifications, there is no need for a protocol.
a source to share
1.If you would like to use any of the UIalertView delegate methods in your .m file, then you must add the UIAlertViewDelegate to your interface. 2. Since you have not added the UIAlertViewDelegate to your interface file, I don’t think you have used any of these DelegView Delegate methods. In this case, you don't need to set your delegate yourself. The delegate can be absent if you do not want to use any of the delegate's methods.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
message:@"<Alert message>"
delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles: nil];
a source to share
Yes, it works, and yes, you must add the protocol.
This works because, well, how Objective C works. You can send any message to any object, although of course the object you are sending the message to may not understand it.
Indeed, the reason is that you have to add a protocol. You tell the compiler that you plan to inject the code, and if you're lucky, it should tell you that you missed something by accident. It is almost always better to know if you missed a method at compile time than at run time.
a source to share