开发者

Something like .NET's "tag" property for Interface Builder

I'm currently struggling to use UI elements in Interface Builder. I keep trying to do things "in the .NET way."

I have several buttons that all map down their TOUCH event to the SAME FUNCTION:

-(IBAction) onTouch:(id) sender
{
  // do something with touch, DEPENDING ON WHAT BUTTON WAS PUSHED
  // I want to do something like
  if( sender.tag == "something" ) 
  {
     //...doesn't work on apple, of course..
  }
}

I want to uniquely identify each BUTTON USING SOMETHING li开发者_如何学编程ke the TAG property in .NET. I tried using the INTERFACE BUILDER "NAME" field that is on the "Identity" panel of interface builder, but I don't know how to access that field programmatically.

-(IBAction) onTouch:(id) sender
{
  // do something with touch, DEPENDING ON WHAT BUTTON WAS PUSHED
  // I want to do something like
  if( sender.InterfaceBuilderName == "something" ) 
  {
     //...doesn't work..
  }
}

So, WHAT / IS THERE a way to uniquely identify a UI element (such as a button) OTHER THAN doing something like

-(IBAction) onTouch:(id) sender
{
  // look at
  [sender currentTitle]
}

The reason that's bad is because if the text on the button changes for some cosmetic reason you break the whole app, right

The last solution I can think of is write seperate functions for each button's touch event but I really want to know if it is possible to uniquely identify a button by something similar to .Net's TAG property.


In the iPhone SDK all UIView objects have a property also called tag which is an integer value and can basically be used to do what you are intending.

I usually define a constant for the tag values I'm going to use for a specific purpose.

You can access the tag on the button object:

myButton.tag = MYBUTTON_TAG_CONSTANT

// button tag constant
#define MYBUTTON_TAG_CONSTANT 1


For buttons, there is a Tag entry in the View section (click on your button, select Attributes Inspector from the Tools menu). You can then use this integer value in your code.

Here is a link that may help as well:

http://www.iphonedevsdk.com/forum/iphone-sdk-development/25582-using-tags-interface-builder.html


UIView's tag property is accessible from Interface Builder. Unlike .NET, it's an integer rather than a string.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜