【发布时间】:2015-10-26 21:43:19
【问题描述】:
我使用[self.view viewWithTag] 来引用 UIViewController 中的对象。然后我继续将特定的子视图移动到新的 UIViewController,然后将其添加为子视图控制器并将视图添加为子视图。但是,现在我无法在子视图控制器中使用viewWithTag 来访问不同的对象。每次使用viewWithTag检索到的对象都是nil。
我不认为我使用 viewWithTag 不当,因为在我将该视图移动到新视图控制器之前它工作正常。一种解决方案是为我使用viewWithTag 引用的每个视图创建属性,但我认为这不是一个好方法。有更好的解决方案吗?为什么会这样?
根据要求进行编辑:
这是我添加子视图控制器的方法:
self.runeTable = [RuneTableViewController new];
[self addChildViewController:self.runeTable];
self.runeTable.view.frame = CGRectMake(screenshotWidth + 32, navBarHeight, self.view.frame.size.width-screenshotWidth-32.0, self.view.frame.size.height-navBarHeight);
[self.view addSubview:self.runeTable.view];
这里是创建按钮的代码:
UIButton *updateButton = [UIButton buttonWithType:UIButtonTypeCustom];
updateButton.tag = 5;
[updateButton setTitleColor:HIGHLIGHT_COLOR forState:UIControlStateNormal];
[updateButton.titleLabel setFont:FONT_MED_LARGE_BOLD];
[updateButton setTitle:@"Update" forState:UIControlStateNormal];
updateButton.frame = CGRectMake(283, 280-60, 100, 60);
[detailInnerView addSubview:updateButton];
在其他方法中,我正在尝试检索目标并将其添加到该按钮:
UIButton *updateButton = (UIButton *)[self.view viewWithTag:5];
[updateButton addTarget:self action:@selector(updateCurrentDictionaryWithRuneInfo) forControlEvents:UIControlEventTouchUpInside];
当我添加断点和po updateButton 时,它返回为零。视图控制器中的其他 UI 元素也发生了同样的事情,我也尝试以这种方式检索。
【问题讨论】:
-
一些显示您添加子视图控制器的位置和方式以及如何尝试获取所需视图的代码将很有用。
-
好的,我会补充的。
-
在这种情况下
self是什么UIButton *updateButton = (UIButton *)[self.view viewWithTag:5];? -
po self返回子视图控制器的实例,RuneTableViewController。
标签: ios null childviewcontroller viewwithtag