我的iPad iOS项目中的标签值面临着奇怪的问题
我在界面构建器
中给出了我的视图的不同子视图标签1,2,3,4,5 ....但是当我试图动态获取标记值时,它会给我0值。
NSArray *pSubviews = [self.view subviews];
// Enumerate the portrait view
for (int sv = 0; sv < [pSubviews count]; sv++)
{
UIView *pObject = (id)[pSubviews objectAtIndex : sv];
NSLog(@"tag %d",pObject.tag);
}
结果
2016-05-07 22:12:58.832[1562:47169] tag 0
2016-05-07 22:12:58.832[1562:47169] tag 0
2016-05-07 22:12:58.832[1562:47169] tag 0
2016-05-07 22:12:58.832[1562:47169] tag 0
2016-05-07 22:12:58.832[1562:47169] tag 0
2016-05-07 22:12:58.832[1562:47169] tag 0
我在app delegate启动方法中为iPad视图动态设置我的视图。
myViewController *ldVC;
if (IS_IPAD)
{
ldVC = [[myViewController alloc]initWithNibName:@"myViewController_iPad" bundle:[NSBundle mainBundle]];
}
else
{
ldVC = [[myViewController alloc]initWithNibName:@"myViewController" bundle:nil];
}
UINavigationController *navVC = [[UINavigationController alloc]initWithRootViewController:ldVC];
self.window.rootViewController=navVC;
查看层次结构::
答案 0 :(得分:2)
如果您在viewDidLoad方法中运行此视图,则视图可能尚未设置。尝试在viewDidLayoutSubviews
中运行它