Is there anything wrong with creating a helper function to display an AlertView?
I'm still learning Objective C and iPhone development, so I'm willing to take instruction. I was wondering if this is the best way to achieve 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开发者_如何学C:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
This is what I would do anywhere else just wondering if that's 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?
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.
Helper functions are generally stored under their Class that you need to create. Therefore creating a Category for UIAlertView makes the most sense.
精彩评论