视图控制器:如何以编程方式在视图之间切换? [英] View Controllers: How to switch between views programmatically?
问题描述
简而言之:我想要两个全屏视图,我可以在视图 A 和视图 B 之间切换.我知道我可以只使用 Tab Bar Controller,但我不想.我想看看这是如何手动完成的,以了解幕后发生的事情.
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,我将它作为子视图添加到 Nib 的视图"中.它是绿色的,我全屏看到它.工作正常.
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 的视图.但现在最大的问题是:视图控制器通常具有所有这些方法,例如:
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?我相信 Tab Bar Controller 在幕后拥有所有这些聪明".还是我错了?
...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];
这篇关于视图控制器:如何以编程方式在视图之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!