Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 为UIAlertView编写函数?_Iphone_Objective C_Uialertview - Fatal编程技术网

Iphone 为UIAlertView编写函数?

Iphone 为UIAlertView编写函数?,iphone,objective-c,uialertview,Iphone,Objective C,Uialertview,我讨厌写基本的UIAlertView,即: UIAlertView *alert = [[UIAlertView alloc] initWith...]] //etc 不这样做,是否可以将所有这些放在一个“helper”函数中,在该函数中我可以返回buttonIndex,或者通常返回的任何警报 对于一个简单的helper函数,我想您可以为标题、消息提供参数,但我不确定您是否可以在参数中传递委托,或者传递捆绑信息 在伪代码中,可能是这样的: someValueOrObject = Print_A

我讨厌写基本的UIAlertView,即:

UIAlertView *alert = [[UIAlertView alloc] initWith...]] //etc
不这样做,是否可以将所有这些放在一个“helper”函数中,在该函数中我可以返回buttonIndex,或者通常返回的任何警报

对于一个简单的helper函数,我想您可以为标题、消息提供参数,但我不确定您是否可以在参数中传递委托,或者传递捆绑信息

在伪代码中,可能是这样的:

someValueOrObject = Print_Alert(Title="", Message="", Delegate="", Bundle="") // etc
CCAlertView *alert = [[CCAlertView alloc]
    initWithTitle:@"Test Alert"
    message:@"See if the thing works."];
[alert addButtonWithTitle:@"Foo" block:^{ NSLog(@"Foo"); }];
[alert addButtonWithTitle:@"Bar" block:^{ NSLog(@"Bar"); }];
[alert addButtonWithTitle:@"Cancel" block:NULL];
[alert show];
这方面的任何帮助都会很好


谢谢

这是我写的,当我厌倦了做同样的事情时:

-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName {
  [self alert: title withBody: message firstButtonNamed: firstButtonName withExtraButtons: nil informing: nil];
}

-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName informing:(id)delegate {
  [self alert: title withBody: message firstButtonNamed: firstButtonName withExtraButtons: nil informing: delegate];
}

-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName withExtraButtons:(NSArray *)otherButtonTitles informing:(id)delegate {
  UIAlertView *alert = [[UIAlertView alloc]
              initWithTitle: title
              message: message
              delegate: delegate
              cancelButtonTitle: firstButtonName
              otherButtonTitles: nil];
  if (otherButtonTitles != nil) {  
    for (int i = 0; i < [otherButtonTitles count]; i++) {
      [alert addButtonWithTitle: (NSString *)[otherButtonTitles objectAtIndex: i]];
    }
  }
  [alert show];
  [alert release];
}
-(void)警报:(NSString*)标题和正文:(NSString*)消息firstButtonNamed:(NSString*)firstButtonName{
[自我提醒:标题带正文:消息firstButtonName:firstButtonName带附加按钮:无通知:无];
}
-(void)警报:(NSString*)标题和正文:(NSString*)消息firstButtonNamed:(NSString*)firstButtonName通知:(id)委托{
[自我提醒:标题和正文:消息firstButtonName:firstButtonName和附加按钮:无通知:代表];
}
-(void)警报:(NSString*)标题和正文:(NSString*)消息第一个按钮名称:(NSString*)第一个按钮名称和附加按钮:(NSArray*)其他按钮名称通知:(id)委托{
UIAlertView*警报=[[UIAlertView alloc]
initWithTitle:title
信息:信息
代表:代表
取消按钮名称:第一个按钮名称
其他按钮:无];
如果(OtherButtonTiles!=nil){
对于(int i=0;i<[OtherButtonTiles count];i++){
[alert addButtonWithTitle:(NSString*)[otherButtonTitles对象索引:i]];
}
}
[警报显示];
[警报发布];
}
但是,您不能编写一个函数来显示一个警报,然后返回一个像buttonIndex这样的值,因为只有当用户按下按钮并且您的代理执行某些操作时,才会返回该值


换句话说,使用UIAlertView询问问题的过程是一个异步过程。

在4.0+中,您可以使用块简化警报代码,有点像这样:

someValueOrObject = Print_Alert(Title="", Message="", Delegate="", Bundle="") // etc
CCAlertView *alert = [[CCAlertView alloc]
    initWithTitle:@"Test Alert"
    message:@"See if the thing works."];
[alert addButtonWithTitle:@"Foo" block:^{ NSLog(@"Foo"); }];
[alert addButtonWithTitle:@"Bar" block:^{ NSLog(@"Bar"); }];
[alert addButtonWithTitle:@"Cancel" block:NULL];
[alert show];

请参阅。

这可以更好地标记。考虑添加<代码> Objul-C<代码> >代码> iPhone < /COD>等等。谢谢你的帮助!非常圆滑的解决方案。对我来说效果很好。在不使用
LambdaAlert
类的情况下,有没有办法做到这一点?你是说使用vanilla
UIAlertView
类?不需要。您必须围绕它编写一些支持代码,这正是
LambdaAlert
所做的。你为什么要避免它?