Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the view a UIButton is on

I have a UIButton on a Custom UITableViewCell, i have a method "done".

How can i get the CustomTableViewCell via the Button?

-(IBAction)done:(id)sender {
    (CustmCell((UIButton)sender).viewTheButtonIsOn...).action
}
like image 691
philipp Avatar asked Dec 02 '12 01:12

philipp


People also ask

How can I check if UIButton is pressed?

You can use the following code. class ViewController: UIViewController { var gameTimer: Timer! @IBAction func btnClick(_ sender: Any) { let button = UIButton() if (button. isSelected) { print(" Not Selected"); } else { print(" Selected"); } } override func viewDidLoad() { super.

What is a UIButton?

A control that executes your custom code in response to user interactions.

How do you highlight a button in Swift?

In storyboard choose the UIButton you want to change. In the identity inspector in the right corner there is a filed named "class" there type "HighlightedButton" and press enter. Now your button will change color to red when it is highlighted and back to green when you release the button.


2 Answers

The answer by CodaFi is most likely enough but it does make the assumption that the button is added directly to the table cell. A slightly more complicated but safer bit of code could be something like:

-(IBAction)done:(id)sender {
    UIView *parent = [sender superview];
    while (parent && ![parent isKindOfClass:[CustomCell class]]) {
        parent = parent.superview;
    }

    CustomCell *cell = (CustomCell *)parent;
    [cell someAction];
}
like image 116
rmaddy Avatar answered Sep 23 '22 10:09

rmaddy


If it is added directly to the cell as a subview, you can use -superview to get it's parent view. Also, you need to cast with pointers because objects are never taken by value, only pointed to in Objective-C.

-(IBAction)done:(id)sender {
    [(CustmCell*)[(UIButton*)sender superview]someAction];
}
like image 22
CodaFi Avatar answered Sep 22 '22 10:09

CodaFi