Access elements created with UI builder in xcode with Objective C by title or ID Access elements created with UI builder in xcode with Objective C by title or ID xcode xcode

Access elements created with UI builder in xcode with Objective C by title or ID


You can get an element by its tag. If you are in a view controller code and you need to get a button that you tagged as 123 in the interface builder, you can use this code:

UIButton *button123 = [self.view viewWithTag:123];

The element does not need to be a button - it can be any UIView descendant: a label, a text view, a stepper, or anything else.

Please note that a more idiomatic way of accessing elements that you create in the interface builder is through IBOutlets.

Here is how you can add an outlet to your view or view controller: open interface builder in a separate window, control-click the element that you want to add as an outlet, find "referencing outlet/new referencing outlet" in the context menu that drops down, and drag from the black circle into the header of your view or view controller. When you drop the item into the code, you will be prompted for the name of the outlet. Once the outlet is created, you can access its corresponding element through the variable that you created.