Do I need to add <UIAlertViewDelegate> protocol?
I have 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];
}
Which I am then calling from my viewController using [self connectionAlert];
everything works fine, but I am not sure if I should or should not add the <UIAlertViewDelegate>
protocol to my viewController interface.
Currently I have NOT added the protocol and everything seems to be working, is this because I am calling the UIAlertView via self? Should I really be adding the protoco开发者_如何学Gol anyways?
many thanks
gary
I've never personally had to add the UIAlertViewDelegate protocol, so I don't see why you would have to.
You only need the protocol if you wish to access the following methods:
Responding to Actions
– alertView:clickedButtonAtIndex:
Customizing Behavior
– willPresentAlertView:
– didPresentAlertView:
– alertView:willDismissWithButtonIndex:
– alertView:didDismissWithButtonIndex:
Canceling
– alertViewCancel:
If you don't want to listen to these notifications, no need for the protocol.
1.If you would like to use any of the UIalertView delegate methods in your .m file, then you should add UIAlertViewDelegate to your interface. 2.Since you haven't added UIAlertViewDelegate in your interface file, I dont think you have used any of those alertView Delegate methods. In this case, you need not set your delegate to self. The delegate can be nil if you do not want to use any of the delegate methods.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
message:@"<Alert message>"
delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles: nil];
Yes it works and yes you should add the protocol.
It works because, well, that's how Objective C works. You can send any message to any object, though of course the object you send a message to may not understand it.
Indeed, that's the reason that you should add the protocol. You're telling the compiler that you plan to implement the code and -- if you're lucky -- that it should tell you if you missed something by accident. It's almost always better to find out if you missed a method at compile time than at run time.
Yes you should. It will work without it but you need to do this because you set delegate object. And may be you want to implement some delegate methods. If you don't then set delegate to nil.
精彩评论