views:

112

answers:

2

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 protocol anyways?

many thanks

gary

+2  A: 

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.

Tilo Mitra
A: 

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.

Skie
For a basic alert view popup, I don't understand the advantage of setting a delegate protocol, yet not accessing any of the delegate methods
Tilo Mitra
As I say if you don't use delegate and set it to nil, you can ignore protocol.
Skie
So I could miss out the protocol, but I would need to set the delegate to "nil" rather than "self" as I have it now. Is that correct?
fuzzygoat
Setting delegate to nil and not specifying a delegate are the same things. The delegate is nil by default. However, for code correctness, you can set it to nil.
Tilo Mitra