首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIAlertAction -设置后编辑UIAlertAction

UIAlertAction -设置后编辑UIAlertAction
EN

Stack Overflow用户
提问于 2017-02-21 18:20:47
回答 2查看 613关注 0票数 1

我有一个名为ManagerClass的类。

Manager类有一个函数showUIAlertController:

代码语言:javascript
复制
- (UIAlertController*)showUIAlertController:(NSString *)title  message:(NSString *)message actions:(NSArray<UIAlertAction*>* )actions

此函数应显示带有接收到的参数的报警控制器。

到目前为止一切顺利..。

现在我想采取这些行动,并以某种方式编辑它们。类似于:

代码语言:javascript
复制
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“并不存在--它只是让你理解我想要实现的目标。

有谁知道如何完成这项任务吗?

在查看苹果的UIAlertAction应用程序接口https://developer.apple.com/reference/uikit/uialertaction时,我没有找到这样做的方法

EN

回答 2

Stack Overflow用户

发布于 2017-02-21 18:24:33

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

代码语言:javascript
复制
[self performSelector:@selector(aMethod:)];

或者在使用以下命令发送对象时:

代码语言:javascript
复制
[self performSelector:@selector(aMethod:)
       withObject:(id)object];

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

  • 编辑*

UIAlertController* alert = UIAlertController alertControllerWithTitle:@“我的警报”消息:@“这是一个警报。”首选样式:UIAlertControllerStyleAlert;

UIAlertAction* defaultAction = UIAlertAction动作addAction:@“确定”样式:UIAlertAction默认处理程序:^(UIAlertAction*动作){ NSLog(@"42.");};报警添加动作:默认动作;自呈现presentViewController:报警动画:YES presentViewController

控制台退出42。取而代之的是你需要的每一个动作。

票数 0
EN

Stack Overflow用户

发布于 2017-02-21 19:11:22

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

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

代码语言:javascript
复制
- (UIAlertController*)showUIAlertController:(NSString *)title  message:(NSString *)message actions:(NSArray<MyActionModel*>* )actions

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

代码语言:javascript
复制
@interface MyActionModel: NSObject {
    @property NSString * title;
    @property UIAlertActionStyle * style;
    @property ((void)^(UIAlertAction *)) action;
}

然后,您可以在需要时创建UIAlertAction,也可以添加到管理器回调中。

另外,如果我的Objective-C不太正确,很抱歉,我有点生疏了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42364608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档