如何从不同方法中删除视图

时间:2012-02-04 18:13:44

标签: objective-c xcode

这是我的代码,可以在我的主窗口上加载两个不同的自定义视图。我的问题是当我按下一个按钮来调用我的PageTwo方法时,我要求它从PageOne中删除我当前的视图并将其替换为新的“text1Title2”。问题是这不起作用,除非我重构我的代码并将其放在一个方法中,在这种情况下我需要一个switch语句,我不知道如何做到这一点。有没有办法从其他方法中删除视图或在调用PageTwo方法时释放该方法?

- (void) PageOne
    {
        NSLog(@"FirstPart");

        WelcomeScreenText1* text1ViewController =
        [[WelcomeScreenText1 alloc] initWithNibName:text1Title bundle:nil];
        if (text1ViewController != nil)
        {
            myCurrentViewController = text1ViewController;  
        }

        // embed the current view to our host view
        [myTargetView addSubview: [myCurrentViewController view]];

        // make sure we automatically resize the controller's view to the current window size
        [[myCurrentViewController view] setFrame: [myTargetView bounds]]; 
    }

    - (void) PageTwo
    {

        if ([myCurrentViewController view] != nil)
            [[myCurrentViewController view] removeFromSuperview];   // remove the current view

        if (myCurrentViewController != nil)
            [myCurrentViewController release];

        WelcomeScreenText2* text2ViewController =
        [[WelcomeScreenText2 alloc] initWithNibName:text1Title2 bundle:nil];
        if (text2ViewController != nil)
        {
            myCurrentViewController = text2ViewController;  
        }

        // embed the current view to our host view
        [myTargetView addSubview: [myCurrentViewController view]];

        // make sure we automatically resize the controller's view to the current window size
        [[myCurrentViewController view] setFrame: [myTargetView bounds]];

    }

1 个答案:

答案 0 :(得分:0)

你可以简单地使用UIViews(而不是UIViewControllers)。将它们作为子视图添加到主视图中,并根据需要隐藏它们。

-(void)switch {
   textView1.hidden = !textView1.hidden;
   textView2.hidden = !textView2.hidden;
}