我有一个继承自ContainerSegue
的自定义UIStoryboardSegue
。在perform()
方法中,我需要访问执行当前segue的UIView
。我怎样才能做到这一点?有可能吗?
换句话说,我需要访问
perform()
方法中的发件人。
答案 0 :(得分:3)
segue
课程通常不跟踪sender
,但由于您已定义了名为segue
的自定义ContainerSegue
课程,因此您可以添加sender
属性:
class ContainerSegue: UIStoryboardSegue {
// Add this property to hold the sender
var sender: AnyObject?
override func perform() {
if let button = sender as? UIButton, title = button.currentTitle {
print("button title is \(title)")
}
// Add remainder of perform code here
}
}
然后在prepareForSegue
中设置:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let containerSegue = segue as? ContainerSegue {
containerSegue.sender = sender
}
}
以类似的方式,如果您要访问sender
中的destinationViewController
,请向sender
添加destinationViewController
属性并在prepareForSegue
中设置该属性}:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let dvc = segue.destinationViewController as? MyDestinationVC {
dvc.sender = sender
}
}
答案 1 :(得分:0)
let button = sender as! UIButton
let view = button.superview