2017-02-21 2 views
0

私はManagerClassという名前のクラスを持っています。UIAlertAction - UIAlertActionが設定された後に編集します

マネージャークラスは、関数showUIAlertControllerがあります

- (UIAlertController*)showUIAlertController:(NSString *)title message:(NSString *)message actions:(NSArray<UIAlertAction*>*)actions 

この機能は、受信したパラメータで警告コントローラが表示されるはずです。

これまでのところは良い...

今私は、これらのアクションを取り、何とかそれらを編集したいと思います。ような何か:

UIAlertAction *action = actions.firstObject; 
UIAlertAction *actionCopyWithAdditionalAction = [UIAlertAction actionWithTitle:action.title style:action.style handler:^(UIAlertAction * _Nonnull action) { 

    [action "performTheAction"]; //perform the original action 
    [ManagerClass doSomething];   
}]; 

は「performTheActionは」存在しない - それはあなたが、私が達成しようとしていますかを理解するだけのためのものです。

は、誰もが、このタスクを達成することができる方法のアイデアを持っていますか?

は、AppleのUIAlertActionのAPI https://developer.apple.com/reference/uikit/uialertaction

答えて

0

を見ながら、あなたのコードで提供される方法を実行することを意味していますことを実行する方法を見つけることができませんでした。でオブジェクトを送信するときに

[self performSelector:@selector(aMethod:)]; 

のか:次に使用

[self performSelector:@selector(aMethod:) 
     withObject:(id)object]; 

注、ここで自己が同じクラスに参照され、それは同様にどこか別の場所である可能性があります。

  • 編集*

    UIAlertController *警告= [UIAlertController alertControllerWithTitle: "マイアラート" @ メッセージ: "これは警告です" @ preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { 
          NSLog(@"42."); 
          }]; 
    
    [alert addAction:defaultAction]; 
    [self presentViewController:alert animated:YES completion:nil]; 
    

コンソールログアウト42は、代わりにすべてのアクションを入れて、あなたが必要なのです。

+0

いいえ、私は1つのUIAlertActionを取り、それを何らかの形で新しいアクションにコピーし、その新しいアクション内で受け取ったアクション+マイコードを実行することを意味します。 –

0

あなただけの最初の警告を表示する第2の警告を呼び出し、あなたのコードの一部を実行したいのはなぜ?最初の警告でもそれを行うことができます。

//Create the UIAlertController 
UIAlertController *theAlertController = [UIAlertController alertControllerWithTitle:@"Your Title" message:@"Your Message" preferredStyle:UIAlertControllerStyleAlert];

//Add an UIAlertAction which the user can click at [theAlertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { //Execute your own code //[self myOwnCode];

//Close the AlertController after your code [self dismissViewControllerAnimated:YES completion:nil]; }]]; dispatch_async(dispatch_get_main_queue(), ^{ [self presentViewController:theAlertController animated:YES completion:nil]; });

私はあなたのことを正しく理解しています。

+0

UIAlertControllerマネージャが必要だとします。そのようなことは、複数の警告が互いに重ね合わされて表示されることを防止する責任があります。したがって、各アクションについて、マネージャはアラートが解除されたことを知りたいと考えます。 可能な方法は、マネージャに以下の機能を持たせることです: - (UIAlertController *)showUIAlertController:(NSString *)タイトルメッセージ:(NSString *)メッセージアクション:(NSArray *)アクション そして、マネージャでUIAlertControllerを作成し、アクションが取られたことをマネージャに知らせるいくつかのコードを各アクションに追加します。 私はそれが意味をなさないことを願っています:) –

0

あなたが代わりにUIAlertActionのアラートアクションモデルを渡すことができます。

ので、あなたの方法は、このようなものになります。MyActionModelは3つのプロパティ

@interface MyActionModel: NSObject { 
    @property NSString * title; 
    @property UIAlertActionStyle * style; 
    @property ((void)^(UIAlertAction *)) action; 
} 

を持つクラスです次に、あなたがそれらを必要とするとき、あなたのUIAlertAction Sを作成してもで追加することができます

- (UIAlertController*)showUIAlertController:(NSString *)title message:(NSString *)message actions:(NSArray<MyActionModel*>*)actions 

をあなたのマネージャコールバック。

P.S.申し訳ありませんが、Objective-Cが正しくない場合、私は少し錆びています。

+0

私はこのアプローチを多かれ少なかれました。しかし、入力または出力パラメータなしで 'runningBlock'を使用しました。この方法で私はUIAlertActionを作成し、ブロック内に 'runningBlock'を実行します。 –

+0

AppleはUIAlertActionを編集する方法を提供していないようです。 –

関連する問題