6

基本的な UIAlertView を書くのにうんざりしています。

UIAlertView *alert = [[UIAlertView alloc] initWith...]] //etc

これを行う代わりに、これらすべてを「ヘルパー」関数に入れ、buttonIndex、または通常アラートが返すものを返すことができますか?

単純なヘルパー関数の場合、タイトル、メッセージのパラメーターをフィードできると思いますが、パラメーターでデリゲートを渡すことができるかどうか、または情報をバンドルできるかどうかはわかりません。

擬似コードでは、次のようになります。

someValueOrObject = Print_Alert(Title="", Message="", Delegate="", Bundle="") // etc

これに関するヘルプは素晴らしいでしょう。

ありがとう

4

2 に答える 2

14

4.0 以降では、次のように、ブロックを使用してアラート コードを簡略化できます。

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

GitHub の Lambda アラートを参照してください。

于 2010-06-15T10:02:13.343 に答える
2

これは私が同じことをするのにうんざりしたときに書いたものです:

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

ただし、アラートを表示してから buttonIndex のような値を返す関数を作成することはできません。これは、ユーザーがボタンを押してデリゲートが何かを実行したときにのみ値が返されるためです。

つまり、 で質問するプロセスはUIAlertView非同期プロセスです。

于 2010-06-10T07:25:34.680 に答える