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
}
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.
A control that executes your custom code in response to user interactions.
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.
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];
}
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With