Several functions of UIButtons 1

Basically, I need to have some buttons in the view. I would like all of them to call one function so that I can track the status.

How can I determine which button is called a function? Is there any way to get the sender text?

+3
source share
3 answers

In iOS action methods, including IBAction methods, there can be any of the following signatures (see " Target-Action in UIKit "):

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event

, , , . , . , , , .

, , if (sender == self.nextPageButton) , if (sender.tag == 4) if ([((UIButton *)sender).currentTitle isEqualToString:@"foo"]). IB , , . , .

+5

tag .

Interface Builder ( ).

:

if (sender.tag == 0) {
} else if (sender.tag == 1)

.

+3

You do not need to explicitly set the tag. You can define the IBOutlets UIButton in your .h file and their properties, as well as

@property (nonatomic , retain) IBOutlet UIButton *myButton;

and method how

-(IBAction) browse : (id) sender; 

in the .m file you can implement the method as

-(IBAction) browse : (id) sender{

    if((UIButton *)sender == myButton){/*add the action here*/}
 } 

Add as many buttons to the if statement as you want. Connect the IBOutlets of all the relevant buttons, and also select the selector.

Remember to free IBOutlets in the dealloc method to prevent memory leaks.

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1794275/


All Articles