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);
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 IBOutlet
s.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With