views:

71

answers:

2

Hello all.
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 others. 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.

+1  A: 

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]; 

}
vodkhang
Close. You can't return that because you've defined the method to return void. Change void to UIAlertView* and that will work. Alternately, don't return anything and add "show" as a final method on the chain after the autorelease.
John Franklin
Sorry, it was my mistake. At the first time, I thought I should show it immediately after alloc init but then I changed:)
vodkhang
Ok, forgive my noobness. So I have my class like this@interface MetodosGerais : NSObject{...}with that method- (UIAlertView *)getUIAlertViewWithDelegate:(id)delegate title:(NSString *)title cancelTitle:(NSString *)cancel;and in my view controller I callMetodosGerais *alerta;[alerta getUIAlertViewWithDelegate:??? title:@"Triceps" cancelTitle:@"OK"];I`m new to OOB and objective-c and I`m having a little trouble understanding delegates. What would be the delegate in this case?TIA
Daniel Carneiro
The delegate should be self. Ok, to understand easily, do you know about composition in OOP. A delegate is a kind of composition but : 1/ you have to specify a method for that object (here is UIAlertView) to call u after it finishes its job. 2/ delegate should usually be id. Is it clear?
vodkhang
All right, it works wen I put it inside my app delegate. but, could I do it in a common.m class for example?
Daniel Carneiro
Ok, I managed to do it. I comformed my common.m clas to the <UIAlertViewDelegate> protocol. It works, but I`m receiving an alert saying "warning: type 'id <UIApplicationDelegate>' does not conform to the 'UIAlertViewDelegate' protocol"
Daniel Carneiro
A: 

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"];
}...
Daniel Carneiro
Ok, this is still not working. The program stil searches the method inside my appDelegate instead of common.m.How can I do to get it to search on my common.m?
Daniel Carneiro