views:

22

answers:

2

I'm still learning Objective-c and Iphone Dev so I'm willing to take instruction. I was wondering if this is the best way to achive a helper function that displays an alertview. Obviously in my code there are various places (report errors, bad data etc) where I wish to display an alert to my user. Rather than create an AlertView object show and release it every time I wish to do that I created a helper function as below

- (void)displayAlertWithTitle:(NSString *)title Message:(NSString *)message 
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
        message:message
        delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];

}

This is what i would do anywhere else just wondering if thats the done thing in Objective-c?

So the next question is, if I want to be able to use this function anywhere in my app, where should I store it, AppDelegate? Singleton Class? or a Category of NSString?

+2  A: 

I don't think there's anything wrong with that.

To access that same method everywhere you might want to add it as a category to UIAlertView.

I don't think putting it in the app delegate is a good idea. It doesn't maintain global state so I don't think it makes sense to use a singleton. And I think this has more to do with the alert than the message that's being displayed.

Stephen Darlington
A: 

Helper functions are generally stored under their Class that you need to create. Therefore creating a Category for UIAlertView makes the most sense.

Niall Mccormack