1

我有一个用户导航的 UINavigationController。将特定的 UIViewController 推送到导航堆栈时,导航栏中会出现一个“设置”按钮。当用户单击此按钮时,我想将当前视图/控制器(即屏幕上的所有内容,包括导航栏)翻转到设置视图。

所以我有一个 SettingsViewController,我想从我的 CurrentViewController 翻转到它,它位于 navigationController 堆栈上。

我尝试执行此操作时遇到各种奇怪的行为,属于 SettingsViewController 的 UIView 将开始动画,滑动到位,navigationButtons 移动,没有任何行为像我想的那样。

-(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更像是“最佳实践”。我已经看到了不同的方法,我在查找或理解生命周期文档以及关于该主题的不同线程和帖子时遇到了麻烦。我错过了“最佳实践”的例子。

非常感谢您提供的任何帮助:)

4

1 回答 1

3

如果您想以编程方式创建视图层次结构,则可以在 -loadView 中进行。为此,您必须自己创建视图,添加其所有子视图,然后将其分配给视图属性,如下所示:

- (void)loadView {
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];

    UIImageView *imageBg = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
    [imageBg setImage:[UIImage imageNamed:@"background.png"]];
    [containerView addSubview:imageBg];
    [imageBg release];

    SettingsSubview *switchView = [[SettingsSubview alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
    [containerView addSubview:switchView];
    [switchView release];

    self.view = containerView;
    [containerView release];
}

它有助于理解调用此方法的上下文以及默认情况下它的行为方式。第一次访问 UIViewController 的视图属性时,默认的 getter 方法会调用 -loadView 来延迟加载视图。-loadView 的默认实现从 nib 加载视图(如果指定了)。否则,它会创建一个普通的 UIView 对象并将其设置为控制器的视图。通过覆盖此方法,您可以确保视图的层次结构在第一次访问时完全形成。

-viewDidLoad 应该用于在视图层次结构完全加载后需要进行的任何后续设置。无论视图是从 nib 加载还是在 loadView 中以编程方式构建,都会调用此方法。

于 2010-04-10T20:40:40.837 回答