Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check UIButton has target?

I want to test a UIbutton on a UIView has proper handler.

One method I have seen is:

NSArray *actions = [loginVC.registerBtn actionsForTarget:target forControlEvent:UIControlEventTouchUpInside];

According to apple actionForTraget returns the actions performed on a target object when the specified event occurs.

But other way I saw is having a loop and checking allTargets:

for (id target in loginVC.registerBtn.allTargets) {
        NSArray *actions = [loginVC.registerBtn actionsForTarget:target forControlEvent:UIControlEventTouchUpInside];
        XCTAssertEqual(actions.count, 1);
        SEL handler = NSSelectorFromString(actions[0]);
        XCTAssertTrue([loginVC respondsToSelector:handler]);
    }

According to apple reference:

allTargets : Returns all target objects associated with the control.

What is the difference to not have a loop. When do we need to loop it and when we need to just get actionForTarget?

second part of my question is only we need to check our button has a target or also check which method is connected to perform action as follow:

NSArray *actions = [loginVC.registerBtn actionsForTarget:target forControlEvent:UIControlEventTouchUpInside]; 
XCTAssertTrue([actions containsObject:@"onRegisterButtonPressed:"]);

What is the best practice?

If developers change different method to handle action for UIButton, they must change it in their test as well or do we need to have a test to fail and update test?

like image 450
Bernard Avatar asked Apr 10 '16 11:04

Bernard


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.

How do I add a target to UIButton?

Using a UIControl with a closure callback viewDidLoad() let button = UIButton(type: . system) button. addTarget(self, action: #selector(buttonTapped(_:)), for: . touchUpInside) } @IBAction func buttonTapped(_ sender: UIButton) { print("Button tapped!") } }

What is UIButton Swift?

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

How do I create a button action in Swift?

SwiftUI's button is similar to UIButton , except it's more flexible in terms of what content it shows and it uses a closure for its action rather than the old target/action system. To create a button with a string title you would start with code like this: Button("Button title") { print("Button tapped!") }


1 Answers

You use the loop with allTargets if you want to check all actions for all targets.

You don't use the loop and just use actionsForTarget:forControlEvent: when you only want the actions for a single known target.

For the 2nd part of your question, what you check is up to you. No one can answer that for you. If you only need to be sure there is at least one action for the event, just make sure actions.count is one or more.

If you need to make sure it has an action and you need to make sure the action has a specific name, then the code you posted is what you need.

like image 118
rmaddy Avatar answered Oct 21 '22 10:10

rmaddy