View Controller:如何以编程方式在视图之间切换? [英] View Controllers: How to switch between views programmatically?

查看:77
本文介绍了View Controller:如何以编程方式在视图之间切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之:我希望有两个全屏视图,我可以在视图A和视图B之间切换。我知道我可以使用标签栏控制器,但我不想这样做。我想看看这是如何手工完成的,以了解幕后发生的事情。

In short: I want to have two fullscreen views, where I can switch between view A and view B. I know I could just use an Tab Bar Controller, but I dont want to. I want to see how this is done by hand, for learning what's going on under the hood.

我有一个充当根控制器的UIViewController:

I have an UIViewController that acts as an root controller:

@interface MyRootController : UIViewController {
    IBOutlet UIView *contentView;
}
@property(nonatomic, retain) UIView *contentView;

@end

contentView连接到我添加的UIView作为笔尖视图的子视图。这有绿色,我看到它全屏。工作正常。

The contentView is hooked up to an UIView which I added as an subview to the "view" of the Nib. This has green color and I see it fullscreen. Works fine.

然后,我创建了两个其他视图控制器几乎相同的方式。 ViewControllerA和ViewControllerB。 ViewControllerA有蓝色背景,ViewControllerB有黑色背景。只是看看哪一个是活跃的。

Then, I created two other View Controllers pretty much the same way. ViewControllerA and ViewControllerB. ViewControllerA has a blue background, ViewControllerB has a black background. Just to see which one is active.

所以,在myRootController的实现中,我这样做:

So, in the implementation of myRootController, I do this:

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    ViewControllerA *vcA = [[ViewControllerA alloc] initWithNib];
    [self.contentView addSubview:vcA.view];

    [cvA release];
}

顺便说一句,-initWithNib方法如下所示:

By the way, the -initWithNib method looks like this:

- (id)initWithNib { // Load the view nib
    if (self = [super initWithNibName:@"ViewA" bundle:nil]) {
        // do ivar initialization here, if needed
    }
    return self;
}

这有效。当我启动应用程序时,我会看到ViewControllerA中的视图。但现在最大的问题是:View Controller通常具有以下所有方法:

That works. I see the view from ViewControllerA when I start the app. But now the big question is: A View Controller typically has all those methods like:


  • (void)viewWillAppear:(BOOL)animated;

  • (void)viewDidDisappear:(BOOL)animated;

  • (void)viewDidLoad;

......依此类推。如果我在没有标签栏控制器的情况下以我的方式进行操作,那么这些方法是谁或者是什么,或者如何调用我的意思是:如果我分配了ViewController的类并且视图得到可见,我是否需要注意调用这些方法?它是如何知道viewWillAppear,viewDidDisappear或viewDidLoad的?我相信标签栏控制器具有所有这些聪明的内幕。或者我错了?

...and so on. Who or what, or how would those methods be called if I do it "my" way without a tab bar controller? I mean: If I allocate that ViewController's class and the view get's visible, would I have to take care about calling those methods? How does it know that viewWillAppear, viewDidDisappear, or viewDidLoad? I believe that the Tab Bar Controller has all this "cleverness" under the hood. Or am I wrong?

更新:我已经测试过了。如果我释放视图控制器(例如:ViewControllerA),我将在viewDidDisappear上没有日志消息。只有在分配和初始化ViewControllerA时,我才会得到一个viewDidLoad。但就是这样。所以所有迹象都代表着UITabBarController的聪明之处;)我必须弄清楚如何复制它,对吗?

UPDATE: I've tested it. If I release the view controller (for example: ViewControllerA), I will get no log message on viewDidDisappear. Only when allocating and initializing the ViewControllerA, I get an viewDidLoad. But that's it. So all signs stand for the cleverness of UITabBarController now ;) and I have to figure out how to replicate that, right?

推荐答案

你可以从最简单的removeFromSuperview / insertSubview开始,一点一点地添加代码。

You can begin from the simplest removeFromSuperview/insertSubview and add code to it little by little.


//SwitchViewController.h
#import 
@class BlueViewController;
@class YellowViewController;

@interface SwitchViewController : UIViewController {
    IBOutlet BlueViewController *blueViewController;
    IBOutlet YellowViewController *yellowViewController;
}
- (IBAction)switchViews:(id)sender;
@property (nonatomic, retain) BlueViewController *blueViewController;
@property (nonatomic, retain) YellowViewController *yellowViewController;
@end




//1. remove yellow view and insert blue view
- (IBAction)switchViews:(id)sender {
    if(self.blueViewController.view.superview == nil)
    {
        [yellowViewController.view removeFromSuperview];
        [self.view insertSubview:blueViewController.view atIndex:0];
    }
}

//2. appear=insert, disappear=remove
if(blueViewController.view.superview == nil)
{
    [blueViewController viewWillAppear:YES];
    [yellowViewController viewWillDisappear:YES];

    [yellowViewController.view removeFromSuperview];
    [self.view insertSubview:self.blueViewController.view atIndex:0];

    [yellowViewController viewDidDisappear:YES];
    [blueViewController viewDidAppear:YES];
}

//3. now add animation
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//blue view will appear by flipping from right
if(blueViewController.view.superview == nil)
{
    [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight 
                            forView:self.view cache:YES];

    [blueViewController viewWillAppear:YES];
    [yellowViewController viewWillDisappear:YES];

    [yellowViewController.view removeFromSuperview];
    [self.view insertSubview:self.blueViewController.view atIndex:0];

    [yellowViewController viewDidDisappear:YES];
    [blueViewController viewDidAppear:YES];
}
[UIView commitAnimations];

这篇关于View Controller:如何以编程方式在视图之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆