开发者

How to get Interface Builder name from id

If I name a widget in Interface Builder, and then I write a method that receives click events on that button:

- (IBAction)btnTouchDown:(id)sender
{
  // how can you identify the button here,
  // if several different buttons map
  // their "Touch" event to this same function?
 开发者_Python百科 // I know you can look at its text but that seems really clumsy
  // can I somehow get its INTERFACE BUILDER NAME?

  // I named each uniquely in interface builder,
  // under "Identity"/"Name"
  // Or is my only recourse to tie EACH BUTTON to its own handler function?
}


Click on the button in interface builder, press CMD+2 (Or in info dialog, second tab) and drag the circle from Touch up inside to File's Owner from the yourView.xib window and then it'll let you select one of the methods which has the - (IBAction) method:(id)sender signature. Choose the one you want to associate with that button :)

You say you don't want to make a method for each button, but actually that is the best way in my opinion, rather than having one huge monolithic method which handles each of the buttons and checks each time to see which one of them it is. Using the process I mentioned above, you can specifically tie a handler method to a particular button, keeping things nice, tidy, and logical.

If you really want to do that though, you can also test for the tag number.


you have to define the button as an instance variable in your view controller and then use the IBOutlet pragma to make it known to IB. So for example you would do this in your view controller:

UIButton IBOutlet *mybutton;

After defining it like this it will show up in your IB connections where you can associate it with the appropriate button

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜