【发布时间】:2010-04-10 13:36:45
【问题描述】:
我有一个用户导航的 UINavigationController。 将特定的 UIViewController 推送到导航堆栈时,导航栏中会出现一个“设置”按钮。当用户单击此按钮时,我想将当前视图/控制器(即屏幕上的所有内容,包括导航栏)翻转到设置视图。
所以我有一个 SettingsViewController,我想从我的 CurrentViewController 翻转到它,它位于 navigationController 堆栈上。
尝试执行此操作时,我遇到了各种奇怪的行为,属于 SettingsViewController 的 UIView 将开始动画,滑动到位,导航按钮四处移动,没有我想的那样。
-(void)settingsHandler {
SettingViewController *settingsView = [[SettingViewController alloc] init];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self.navigationController.view
cache:YES];
[self.navigationController.view addSubview:settingsView.view];
[UIView commitAnimations];
}
上面的结果是视图正确翻转,但是 SettingsViewController 的子视图都定位在 (0, 0) 并且在转换之后,它们“卡入”到位?
是不是因为我在viewDidLoad中实例化并添加了我的子视图,像这样?
- (void)viewDidLoad {
UIImageView *imageBg = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
[imageBg setImage:[UIImage imageNamed:@"background.png"]];
[self.view addSubview:imageBg];
[imageBg release];
SettingsSubview *switchView = [[SettingsSubview alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
[self.view addSubview:switchView];
[switchView release];
[super viewDidLoad];
}
1:我应该如何正确地进行“翻转”转换,从 UINavigationController 中的 UIViewController 到新的 UIViewController,然后从新的 UIViewController 再回到驻留在 UINavigationControllers 堆栈上的“原始”UIViewController ?
2:在将子视图实例化并添加到 UIViewController 时,我是否应该使用与“viewDidLoad”方法不同的方法?
-问题 2 更像是“最佳实践”。我见过不同的方式 这样做,我在查找或理解生命周期文档以及有关该主题的不同线程和帖子时遇到了麻烦。我缺少“最佳实践”示例。
非常感谢您提供的任何帮助:)
【问题讨论】:
标签: uiview uiviewcontroller uianimation