Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out the parent view of a clicked button in swift

Tags:

ios

swift

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

 @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

like image 882
Katakam Nikhil Avatar asked Jun 19 '15 18:06

Katakam Nikhil


2 Answers

Please try the following

sender.superview
like image 140
Vakas Avatar answered Nov 12 '22 21:11

Vakas


I you don't want to create a custom class and also don't want to use 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]
    ...
}
like image 45
azm882 Avatar answered Nov 12 '22 20:11

azm882