UIAlertAction - 设置后编辑UIAlertAction

时间:2017-02-21 10:20:47

标签: ios objective-c uialertcontroller

我有一个名为ManagerClass的类。

Manager Class有一个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];         
}];

&#34; performTheAction&#34;不存在 - 只是为了让你理解我想要实现的目标。

有谁知道如何实现这项任务?

在查看Apple的UIAlertAction API时没有找到办法 https://developer.apple.com/reference/uikit/uialertaction

3 个答案:

答案 0 :(得分:0)

您的意思是执行代码提供的方法。然后使用:

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

或使用以下内容发送对象时

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

注意,这里的self引用了同一个类,它也可以在其他地方引用。

  • 编辑*

    UIAlertController * alert = [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.改为执行您需要的所有操作。

答案 1 :(得分:0)

为什么要调用仅显示第一个警报并执行某些代码的第二个警报?您也可以在第一个警报中执行此操作。

//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]; });

希望我理解你。

答案 2 :(得分:0)

您可以传递警报操作模型而不是UIAlertAction

所以你的方法看起来像这样:

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

其中MyActionModel是一个具有3个属性的类

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

然后,您可以在需要时创建UIAlertAction,并添加经理回调。

P.S。对不起,如果我的Objective-C不太正确,我有点生气。