【问题标题】:Open New view on iPad, and close existing view.在 iPad 上打开新视图,然后关闭现有视图。
【发布时间】:2011-05-12 21:24:26
【问题描述】:

我在 iPad 上打开了一个视图(View1,分配给 View1Controller)。我怎样才能让它打开 View2 (View2Controller) 并自行关闭......我确信它非常简单,但我正在管理弹出窗口并且找不到这么简单的东西。如果可能的话,动画过渡会更好。

感谢您的帮助

【问题讨论】:

    标签: ios ipad uiview uiviewcontroller


    【解决方案1】:

    这里是您可以完成此任务的一种方法的粗略概述。

    假设您的 AppDelegate 中有名为 viewController1viewController2 的属性(这些属性应该是 nonatomic, retain 以使内存管理更容易)。

    当您想从viewController1 切换到viewController2 时,您需要删除以执行以下操作

    // Remove the old view
    [self.viewController1.view removeFromSuperView];
    // Release it's controller (just to economize on memory)
    self.viewController1.view = nil;
    if(self.viewController2 == nil)
    {
        // Load the new controller from it's NIB/XIB
        ViewController2* vc2 = [[ViewController2 alloc] 
                                     initWithNibName:@"ViewController2" 
                                     bundle:nil];
        // Assign it to property & release to keep memory management clean
        self.viewController2 = vc2;
        [vc2 release];
    }
    // Add the new view controller to the window
    [self.window addSubview:vc2];
    

    通知 AppDelegate 切换视图的最简单方法之一是使用通知中心。

    既然您在属性中保留了视图控制器,请不要忘记在您的dealloc 中释放它们。 希望这对您有所帮助。

    【讨论】:

    • 如果我的视图是在没有 nib 的情况下创建的,而是由代码创建的。
    • 只需将 initWithNib 替换为您在问题中创建的 ViewController1ViewController2。 (尽管即使视图是在代码中创建的,您也应该能够将 nil 传递给 initWithNibName,这将导致您的控制器的 loadView 被调用,这是您应该创建编程视图的地方。
    猜你喜欢
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多