如何创建一个返回UIAlertViews的方法?
问题描述:
我有一个关于在屏幕上显示警报的问题。事情是:我有一个20至30个不同的屏幕(笔尖)的应用程序,并在每个笔尖我做一些检查,看看用户是否已经插入文本文本。并且一些警报消息与其他消息相同。就像在3个笔尖中,有一个供用户输入他的年龄的文本字段,以及如果他留下空白的警报。 我想要做的是创建一个方法来显示这些警报,所以我不需要在不同的笔尖上有相同的警报。而不是调用每个笔尖的警报视图,我会调用该方法并传递什么样的alertview弹出。
实施此方法的最佳方法是什么? TIA。
TIA。如何创建一个返回UIAlertViews的方法?
答
你可以只的alloc初始化一个新的UIAlertView中像往常一样,但你要记住,要通过委托在
这里是我的方法:
- (UIAlertView *)getUIAlertViewWithDelegate:(id)delegate title:(NSString *)title cancelTitle:(NSString *)cancel {
return [[[UIAlertView alloc] initWithTitle:title delegate:delegate cancelButtonTitle:cancel otherButtonTitles:nil] autorelease];
}
答
好吧,我设法做到这一点。感谢所有帮助。这是我的最终解决方案
我创建了一个用于各种笔尖的一些方法的common.m类。
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];
}
...
,并在我的课,我想用它我也
Common *myAlert = (Common *)[[UIApplication sharedApplication] delegate];
if ([idade.text length] == 0) {
[myAlert getUIAlertViewWithDelegate:self title:@"Enter Your Age" cancelTitle:@"OK"];
}...
+0
好吧,这仍然无法正常工作。该程序stil搜索我的appDelegate而不是common.m的方法。 我该怎么做才能让它在我的common.m上搜索? – 2010-07-29 18:13:18
关闭。你不能返回,因为你已经定义了返回void的方法。将void更改为UIAlertView *,这将起作用。或者,不要返回任何东西,并在autorelease之后添加“show”作为链上的最终方法。 – 2010-07-28 14:39:58
对不起,这是我的错误。在第一次,我想我应该在alloc初始化后立即显示它,但后来我改变了:) – vodkhang 2010-07-28 14:49:59
好的,原谅我的noobness。所以我有我的班级这样 @interface MetodosGerais:NSObject { ...} 与该方法 - (UIAlertView *)getUIAlertViewWithDelegate:(id)委托标题:(NSString *)标题cancelTitle:(NSString *)取消; 和我在视图中的控制器我打电话 MetodosGerais * alerta; [alerta getUIAlertViewWithDelegate:???标题:@“三头肌”cancelTitle:@“OK”]; 我是OOB和Objective-C的新手,我对理解代表有点麻烦。在这种情况下代表什么? TIA – 2010-07-28 16:49:11