将对象附加到UIActionSheet [英] Attaching an object to a UIActionSheet

查看:98
本文介绍了将对象附加到UIActionSheet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按下视图的删除按钮。该视图属于处理按钮按压的视图控制器。但是,该视图控制器是一个容器视图控制器的子对象,因此它向委托者发送一个消息,表示请求删除,并包含应该删除的对象。

A view's 'delete' button is pressed. The view belongs to a view controller, which handles the button press. However, that view controller is a child of a container view controller, so it sends its delegate a message that a deletion was requested, and includes the object that should be deleted.

代理(父视图控制器)接收通知并显示 UIActionSheet 以确认删除。它也是该操作表的委托。

The delegate (the parent view controller) receives the notification and presents a UIActionSheet to confirm the deletion. It also makes itself the delegate of that action sheet.

用户确认删除,并且父视图控制器准备删除对象。除非在 actionSheet:didDismissWithButtonIndex:中这样做。到那时,它不再知道哪个对象是从子视图控制器传递来的。

The user confirms the deletion, and parent view controller is ready to delete the object. Except it has to do this in actionSheet:didDismissWithButtonIndex:. By that point, it no longer knows which object was passed down from the child view controller.

是否有一种方法可以将对象附加到警报表, Objective-C 2.0运行时支持关联的对象 - 使用这个API,你可以使用这个对象来创建一个对象,这个对象可以被获取。

Is there a way to attach an object to the alert sheet so that when it's dismiss action is fired, that object can be retrieved?

推荐答案

您可以使用键值方法,例如,关联对象。示例:

The Objective-C 2.0 runtime supports associated objects - using this API, you can, euh, associate object with each other using a key-value method. Example:

id someObject = // however you obtain it
objc_setAssociatedObject(theActionSheet, "AssociatedDelegateObject", someObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

// later try to get the object:
id someObject = objc_getAssociatedObject(theActionSheet, "AssociatedDelegateObject");
// process the associated object, then release it:
objc_removeAssociatedObjects(theAlertSheet);

编辑:似乎你不需要真正需要拍摄鸟使用大炮和使用运行时函数,因为管理警告表的同一个类/对象也关心委托,因此您可以将它临时分配给一个实例变量。但是,当对象模型变得更复杂时,此方法可能更容易扩展。

it seems that you don't really need to shoot a bird usign a cannon and use runtime functions, since the same class/object that manages the alert sheet cares about the delegation also, so you could just assign it temporarily to an instance variable. However, this approach may be easier to extend later when your object model gets more complicated.

这篇关于将对象附加到UIActionSheet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆