什么是iPhone相当于Android的findViewById(int)?

Dix*_*ine 7 iphone user-interface android ios

我来自Android,刚刚开始学习iOS开发.如何从代码中引用Interface Builder中创建的UI元素?在Android中,这可以通过该功能完成findViewById().

什么是iOS SDK相当于findViewById()

Ahm*_*ali 7

设置控件标签属性,然后使用它的标签检索控件 viewWithTag.

tag:可用于标识应用程序中的视图对象的整数.

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag = 1;
[myView addSubview:button]; 
Run Code Online (Sandbox Code Playgroud)

稍后当您需要检索/访问UIButton控件时:

UIButton* btn = [myView viewWithTag:1];
Run Code Online (Sandbox Code Playgroud)


请记住,您还可以在控件属性Tag中的.xib(界面构建器)文件中设置该属性.

请参阅Apple的UIView类参考标记属性