私は、我々のアプリは、両方をサポートできるようにするためのヘルパークラスを作成しようとしているUIAlertAction
とUIAlertView
。ただし、のalertView:clickedButtonAtIndex:
メソッドを記述しているときに、UIAlertViewDelegate
この問題に遭遇しましたUIAlertAction
。のハンドラブロックでコードを実行する方法がわかりません。
とUIAlertAction
呼ばれるプロパティにsの配列を保持することでこれを実行しようとしていますhandlers
@property (nonatomic, strong) NSArray *handlers;
次に、次のようなデリゲートを実装します。
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIAlertAction *action = self.handlers[buttonIndex];
if (action.enabled)
action.handler(action);
}
ただし、ヘッダーには次のものがあるaction.handler
ため、プロパティはなく、実際にそれをフェッチする方法はありませんUIAlertAction
。
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertAction : NSObject <NSCopying>
+ (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(UIAlertAction *action))handler;
@property (nonatomic, readonly) NSString *title;
@property (nonatomic, readonly) UIAlertActionStyle style;
@property (nonatomic, getter=isEnabled) BOOL enabled;
@end
のhandler
ブロックでコードを実行する他の方法はありUIAlertAction
ますか?
いくつかの実験の後、私はこれを理解しました。ハンドラブロックを関数ポインタとしてキャストでき、関数ポインタを実行できることがわかりました。
そのようです
//Get the UIAlertAction
UIAlertAction *action = self.handlers[buttonIndex];
//Cast the handler block into a form that we can execute
void (^someBlock)(id obj) = [action valueForKey:@"handler"];
//Execute the block
someBlock(action);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加