What is the iPhone equivalent of Android findViewById (int)?

I came from Android and just started studying iOS development. How can I refer to user interface elements created in the interface designer from the code? In android, this can be done using the findViewById () function.

Tpx

+6
source share
3 answers

Set the tag property of the control , and then viewWithTag tag control using viewWithTag .

tag: an integer that can be used to identify view objects in the application.

Example

 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.tag = 1; [myView addSubview:button]; 

Later, when you need to get / access the UIButton control:

 UIButton* btn = [myView viewWithTag:1]; 


Remember that you can also set the Tag property in the .xib file (interface) in the attributes of the control.

see Apple UIView class reference tag property

+6
source

Coming from the Jeff LeMarche school starting iPhone dev, you can also create member variables in your classes, decorate them as an IBOutlet, and bind user interface elements in the interface builder with variables.

Here's a link to his initial book in Google Books, page 52, in stages: http://books.google.com/books?id=TcP2bgESYfgC&lpg=PP1&dq=beginning%20iphone%203%20development&pg=PA52#v=onepage&q=connecting% 20everything & f = false

Xcode 4 does things, it’s much easier to do the same. Just open your .xib file, and on the "Editor" toolbar in the upper right corner, click the middle button "Show Assistant Editor". This should open the associated header (.h) file for that .xib, and you can simply control the drag and drop of the user interface element into the .h file and it will generate IBOutlet code for you.

+2
source

on iOS, you can use the viewWithTag function of the UIView .

 - (UIView *)viewWithTag:(NSInteger)tag 

Use it as below

 UIView* myView12 = [SuperViewOf_myView12 viewWithTag:12] 
0
source

Source: https://habr.com/ru/post/886809/


All Articles