在SWIFT中为UIAlertController设置标签?

Riy*_*ker 3 ios swift uialertcontroller

如何在swift中为警报视图设置标记.我无法设置标签alert.tag = 1,如果我这样做,我会收到类似的错误'UIAlertController' does not have a member named 'tag'.以下是我写的代码,

var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "YES", style: UIAlertActionStyle.Default, handler:{ (ACTION :UIAlertAction!)in }))
alert.addAction(UIAlertAction(title: "NO", style: UIAlertActionStyle.Default, handler:{ (ACTION :UIAlertAction!)in }))

alert.tag = 1 // Error is shown here

self.presentViewController(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

我们曾经在Obj-C中为警报视图设置标签.有人可以帮我这个.提前致谢.

Fog*_*ter 5

UIAlertController没有财产tag.提示是错误"'UIAlertController'没有名为'tag'的成员".

UIAlertController不是UIView它的子类是它的子类UIViewController.

tag即使可用,也几乎没有任何理由可以使用.

您可能想要的是将其设置为属性.

  • 有很多情况下标签非常有用,例如多个tableviews具有相同的委托,多个文本字段与委托等... (2认同)

Wai*_*ain 5

我喜欢@Fogmeister的答案,并希望添加:

您想要tag在警报视图上使用a的唯一原因是识别它,以便您可以区分哪一个正在调用委托方法(没有将每个潜在的内容存储在属性中并检查所有指针的开销).这并不理想,但它是一个快速的解决方案,并且如前所述,它通常是某种形式的黑客.

整个委托方法被替换UIAlertController为基于块的接口,因此不再需要使用tag.您提供给警报操作的块可以self使用专用代码进行引用,并且可以捕获您需要的任何局部变量,因此不需要执行您之前使用过标记的相同操作.