Put three buttons were btn1, btn2, btn3
When you click one of the buttons when the button is clicked to determine how three of which?
Reply:
You first have to declare and define three click event, - (IBAction) cliekBtnOne {/ / event handling}, - (IBAction) cliekBtnTwo {/ / event handling}, - (IBAction) cliekBtnThree {/ / event handling}, and then in IB in their association. Hold down the Ctrl key, the corresponding button pulled File's own, click to select the event you want to!
Reply:
I can only define a click event, - (IBAction) cliekBtn {/ / event handling} in - (IBAction) cliekBtn the judgment which button in the implementation of the event, because the execution of the event almost did not want to write three, do not know can not achieved.
That is more than UIButton function mapped to the same event, in the event, how to distinguish which button triggered the event?
Reply:
Using tag attributes
Reply:
tag attribute how to use?
Thank you
Reply:
Tag will be set in ButtonAttributes 1 (using a UIButton)
click event function
- (IBAction) cliekBtn {
UIButton * button = (UIButton *) sender;
int btnTag = [button tag];
...
}
btnTag always 0 ... What's wrong?
Reply:
Haha, do come out, there is no saving interface
But thank Linux_fay answer
Reply:
First file in * ViewController.h declare IBOutlet UIButton * btn; @ property (retain, nonatomic) UIButton * btn; declare a function - (IBAction) cliekBtn;
Then * ViewController.xib in, the three button is associated to btn, and is associated clickBtn event, then is the button Attributes in three button controls for setting tag, points can be found in the lower half of the button Attrubutes's, tag attributes . For the first button will tag is set to 1, the second set to 2, and the third is set to 3.
Finally * ViewController.m the @ synthesize btn;
Then realized
- (IBAction) cliekBtn {
if (btn.tag == 1) {
/ / Do something
}
else if (btn.tag == 2) {
/ / Do something
} Else {
/ / Do something
}
}
Reply:
Upstairs wrong, I did not go to trial, it was not related to an IBOutlet of colleagues.
First in * ViewController.h file, declare a function - (IBAction) cliekBtn;
Then * ViewController.xib in, the three button is associated to clickedBtn event and set the tag value in the second half of button Attrubutes. The first button on the tag is set to 1, the second set to 2, and the third is set to 3.
Finally, to achieve the click event
in * ViewController.m- (IBAction) cliekBtn {
UIButton * btn = (UIButton *) sender;
int btnTag = [btn tag];
if (btn.tag == 1) {
/ / Do something
}
else if (btn.tag == 2) {
/ / Do something
} Else {
/ / Do something
}
}
Reply:
/ / Int btnTag = [btn tag]; no, comment
The landlord does not know how to achieve?
Reply:
Declaring a function - (IBAction) cliekBtn, the three button is associated to cliekBtn event and set the tag value in the second half of button Attrubutes. The first button on the tag is set to 1, the second set to 2, and the third is set to 3
In clickedBtn event, the first sender's turned into an object
UIButtonUIButton * button = (UIButton *) sender;
In taking the current tag values
buttonint btnTag = [button tag];
Not int btnTag = [btn tag];
No comments:
Post a Comment