Bright
Bright

Reputation: 5751

How to access button names in the storyboard

How do I access this name in code, not the title:

enter image description here

I find this could be useful as an id of any objects on the storyboard.

Upvotes: 2

Views: 4365

Answers (2)

Sweeper
Sweeper

Reputation: 272905

I think in this case, the name that appears in the storyboard is just your outlet name. But in other cases, that name can be lots of things, like the text on your button. Since it can be so many values I doubt there is a way to get it except looking at the source code of Xcode itself. But I think Xcode is not open source.

If you want an id for your view, use tag. Every view has a tag property. You can set it to some integer and use the viewWithTag method to get the view back!

If you insist on using strings as ids, create a dictionary that maps your string id to the tag

Upvotes: 2

Sandeep Bhandari
Sandeep Bhandari

Reputation: 20379

Looks like you are new to iOS,

When you drag a button on to view it will have a default title of "Button" when you check its name it will be "Button", now if you select the button and change its title to "ABCD" and if you see the view hierarchy it will show the button as "ABCD".

Finally if you drag an IBOutlet from button to your VC and name the IBOutlet as "ABCD1234" now its name in view hierarchy is shown as "ABCD1234"

Summary is it is showing the same button, as long as there is no IBOutlet to it or its IBOutlet is named something other than its title its name will be shown as title, when you rename its IBOutlet to different from title its name is shown as the name of IBOutlet.

The concept of ID and then using findViewById("ID string") is not in iOS, if you want to access the view/button you create a IBOutlet to it :)

Upvotes: 4

Related Questions