【问题标题】:Reloading UIView property with view in nib使用 nib 中的视图重新加载 UIView 属性
【发布时间】:2013-08-29 12:16:20
【问题描述】:
我的视图控制器 xib 文件中有一些 UIView - 1、2、3。
View 1 是视图控制器的主视图。
视图 2 和 3(在 xib 中位于主视图 1 之外)也是视图控制器中的属性。因此,我不需要初始化它们。我可以在代码中将它们添加到我的主视图中。
我的问题是:我可以重新加载视图 2 和 3。例如,在我更改了视图 2 中的标签和视图框架之后,我可以重新加载视图以恢复所有原始布局等吗?
【问题讨论】:
标签:
ios
objective-c
uiview
uiviewcontroller
xib
【解决方案1】:
你可以这样使用:
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"MainViewController" owner:self options:nil];
UIView *mainViewControllerSuperView = [nibArray objectAtIndex:0];
NSArray *superViewsArray = [mainViewControllerSuperView subviews];
UIView *secondView = [superViewsArray objectAtIndex:1]; // index of view you need
self.mainViewController.secondView = secondView;
或单行:
self.mainViewController.view = [[(UIView *)[[[NSBundle mainBundle] loadNibNamed:@"MainViewController" owner:self options:nil] lastObject] subviews] objectAtIndex:1];