How to create a method to return UIAlertViews?

I have a question about showing a warning on the screen. The fact is that I have an application with 20-30 different screens (nibs). And in each nib, I do some checking to see if the user inserted text in textedit. And some warning messages are identical to others. As in 3 paragraphs, there is a text box for the user to enter his age, and a warning that appears if he left it empty. What I want to do is create a method to display these warnings, so I don’t need to have the same warning on different tips. instead of calling the warning view in each nib, I would call the method and pass in what kind of alertview will appear.
What would be the best way to implement this method?
TIA.

+3
source share
2 answers

You can simply assign a new UIAlertView as usual, but you must remember to pass a delegate.

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

}
+1
source

Ok, I managed to do this. Thanks to all that helped. This is my final decision.

I created a common.m class for some methods that I use in different 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, I did

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

Source: https://habr.com/ru/post/1756955/


All Articles