开发者

How to create a method to return UIAlertViews?

I have a question about showing an alert on the screen. The thing is: I have an app with 20 to 30 different screens (nibs) And in each nib I do some checking to see if the user has inserted text in a textedit. And some alert messages are identical to othe开发者_C百科rs. Like In 3 nibs there is a text field for the user to enter his age, and the an alert that shows up if he left it blank. What i want to do is to create a method to show these alerts so I don`t need to have the same alert on different nibs. instead of calling an alert view in each nib, I would call the method and pass what kind of alertview to pop up.

What would be the best way to implement this method?

TIA.


You can just alloc init a new UIAlertView as usual but you have to remember to pass the delegate in.

Here is my method:

- (UIAlertView *)getUIAlertViewWithDelegate:(id)delegate title:(NSString *)title cancelTitle:(NSString *)cancel {
    return [[[UIAlertView alloc] initWithTitle:title delegate:delegate cancelButtonTitle:cancel otherButtonTitles:nil] autorelease]; 

}


All right, I managed to do it. Thanks to all that helped. This is my final solution

I created a common.m classs for some methods that I use in various nibs.

Common.h

@interface MetodosGerais : NSObject <UIAlertViewDelegate>{...}
- (void)getUIAlertViewWithDelegate:(id)delegate title:(NSString *)title cancelTitle:(NSString *)cancel;

Common.m

- (void)getUIAlertViewWithDelegate:(id)delegate title:(NSString *)title cancelTitle:(NSString *)cancel {

if (title == @"Enter your Height"){
    [[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Atention!", @"Atenção!")
                                 message:@"You Should enter Your Height."
                                delegate:self 
                       cancelButtonTitle:@"OK" 
                       otherButtonTitles:nil] autorelease] show]; 
}
else if (title == @"Enter your Age"){
    [[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Atention!", @"Atenção!")
                                 message:@"You Should enter your Age."
                                delegate:self 
                       cancelButtonTitle:@"OK" 
                       otherButtonTitles:nil] autorelease] show]; 
}
...

and in my classes that I want to use it I did

Common *myAlert = (Common *)[[UIApplication sharedApplication] delegate];
if ([idade.text length] == 0) {
    [myAlert getUIAlertViewWithDelegate:self title:@"Enter Your Age" cancelTitle:@"OK"];
}...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜