【发布时间】:2013-07-11 14:43:28
【问题描述】:
我有这段代码,它根据在表视图中选择的选项将子视图设置为某个 ViewController 的视图。代码如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self setMainView:[[[self controllerArray] objectAtIndex:[indexPath row]] view]];
[self setCurrentViewController:[indexPath row]];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
最初添加子视图的代码如下所示:
[self setMainView:[[[self controllerArray] objectAtIndex:0] view]];
[[self view] addSubview:[self mainView]]
问题在于更改子视图的代码不会更新子视图,因此视图永远不会真正加载。我可以通过将视图从子视图 ([[self mainView] removeFromSuperview]) 中删除来重新加载视图,但这会导致它重新加载到中心。子视图可以根据用户手势移动,我想把它放在同一个地方。有没有办法重新加载子视图,或者我必须跟踪子视图的位置,然后在删除它并再次添加后设置它。
编辑: 一个有趣的注释: 此代码完美运行(视图停留在先前的坐标):
[[self mainView] removeFromSuperview];
[self setMainView:[[[self controllerArray] objectAtIndex:[indexPath row]] view]];
[[self view] addSubview:[self mainView]];
[self setCurrentViewController:[indexPath row]];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
第一次切换视图除外。我可以设置一次坐标,但有没有更快的方法,所以我不必每次切换时都删除和添加视图。
【问题讨论】:
-
“重新加载”是什么意思?
-
您应该在视图控制器的视图上实现
layoutSubviews方法并调用[self.view layoutIfNeeded]来重新调整所有子视图的位置。 -
@JustinMeiners 该视图不会自动更新到新视图(在我将其设置为新视图后它仍然显示旧视图)。有没有办法更新它。
-
@Eimantas 设置位置不是问题。问题是重新加载内容。
-
@cabellicar123 所以你删除一个视图?添加新视图?它仍然显示旧视图?
标签: ios objective-c cocoa-touch uiview