Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have looked everywhere and I cant seem to figure out how to access elements in my UI in xcode. I know how to change and access elements when I create the UI elements programmatically, but not when I make them using the xcode ui builder.

Simply put: Is there a get element by title or something similar (I do not see ID attributes) and if its there please tell me where it is or how to set it.

Javascript equivalent of what i am trying to do: document.getElementById('ID');

Java equivalent of what i am trying to do: (EditText)findViewById(ID);

like image 709
Osman Avatar asked Jan 01 '13 07:01

Osman


1 Answers

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.

like image 99
Sergey Kalinichenko Avatar answered Oct 22 '22 04:10

Sergey Kalinichenko