ios 快速找出单击按钮的父视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30945442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:31:09  来源:igfitidea点击:

Find out the parent view of a clicked button in swift

iosswift

提问by Katakam Nikhil

I have some code where I have 3 buttons and I would like to find out the parent view of those buttons when clicked

我有一些代码,其中有 3 个按钮,我想在单击时找出这些按钮的父视图

 @IBAction func resizeButtonClicked(sender: UIButton) {
    if(sender.isEqual(resizeButton)) {
        //This is to convert to small square
    } else if(sender.isEqual(maximizeButton)) {
        //This is to convert to maximized view
    } else if(sender.isEqual(closeButton)) {
        //This is to close the view completely

    }
}

Now I can identify the sender button but how do I identify the view this button is sitting in?

现在我可以识别发件人按钮,但如何识别此按钮所在的视图?

Thank you

谢谢

Nikhil

尼基尔

回答by Vakas

Please try the following

请尝试以下操作

sender.superview

回答by azm882

I you don't want to create a custom class and also don't want to use accessibilityHin:

我不想创建自定义类,也不想使用accessibilityHin:

    func performAction(_ sender : AnyObject?)
{
    let cell = sender?.superview??.superviewOfClassType(UITableViewCell.self) as! UITableViewCell
    let tbl = cell.superviewOfClassType(UITableView.self) as! UITableView
    let indexPath = tbl.indexPath(for: cell) 
    let myData = myDataArray[indexPath.row]
    ...
}