「按一下按鈕換背景顏色」聽起來是新手練習題,但它背後其實藏著一個重要的觀念差異:用 rootViewController 管理子控制器,還是直接把控制器的 view 疊在 window 上。這兩種做法在記憶體管理、狀態保存與返回行為上完全不同。
建立一個 root 控制器,內部持有兩個子控制器(紅底與黃底),透過 insertSubview / removeFromSuperview 做切換。這是較正規的寫法。
#import <UIKit/UIKit.h> @class HHLRedViewController; @class HHLYellowViewController; @interface HHLrootViewController : UIViewController @property (retain, nonatomic) HHLRedViewController *pRedVC; @property (retain, nonatomic) HHLYellowViewController *pYellowVC; - (IBAction)switchController:(id)sender; @end
核心判斷是「哪一個子控制器的 view 目前沒有 superview」,那就是尚未顯示的一方,把它換到最底層即可:
- (IBAction)switchController:(id)sender {
if (self.pRedVC.view.superview == nil) {
if (self.pRedVC == nil) {
HHLRedViewController *redVC =
[[HHLRedViewController alloc] initWithNibName:nil bundle:nil];
self.pRedVC = redVC;
[redVC release];
}
[self.pYellowVC.view removeFromSuperview];
[self.view insertSubview:self.pRedVC.view atIndex:0];
} else {
if (self.pYellowVC.view.superview == nil) {
if (self.pYellowVC == nil) {
HHLYellowViewController *yellowVC =
[[HHLYellowViewController alloc] initWithNibName:nil bundle:nil];
self.pYellowVC = yellowVC;
[yellowVC release];
}
[self.pRedVC.view removeFromSuperview];
[self.view insertSubview:self.pYellowVC.view atIndex:0];
}
}
}
viewDidLoad 中先把紅色控制器放進來作為初始畫面,dealloc 中記得釋放兩個子控制器。
HHLrootViewController *rootView1 =
[[HHLrootViewController alloc] initWithNibName:nil bundle:nil];
self.rootView = rootView1;
[rootView1 release];
self.window.rootViewController = self.rootView;
[self.window makeKeyAndVisible];
另一種思路是乾脆不用 root view controller,把兩個控制器的 view 都加到 window,再用 bringSubviewToFront: / sendSubviewToBack: 決定誰在上面。
_viewController1 = [[HHLViewController1 alloc] init]; _viewController2 = [[HHLViewController2 alloc] init]; [_window addSubview:_viewController1.view]; [_window addSubview:_viewController2.view]; [_window bringSubviewToFront:_viewController1.view];
切換按鈕的實作非常短:
- (void)buttonDidPush {
[self.view.window sendSubviewToBack:self.view];
}
兩個子控制器的 view 各自帶一個 Label 與一顆切換鈕,並靠 autoresizingMask 維持版面:
UILabel *label = [[[UILabel alloc] initWithFrame:self.view.bounds] autorelease];
label.text = @"Hello, world";
label.backgroundColor = [UIColor whiteColor];
label.textColor = [UIColor blackColor];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
[self.view addSubview:label];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"畫面跳轉" forState:UIControlStateNormal];
[button sizeToFit];
CGPoint p = self.view.center;
p.y += 50;
button.center = p;
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleBottomMargin;
[button addTarget:self action:@selector(buttonDidPush)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
另外,若只是要「換背景顏色」而非換整個畫面,其實根本不需要兩個 view controller——直接對 self.view.backgroundColor 做動畫切換即可,="#[UIColor colorWithRed:...]" 搭配 UIView 動畫就能做出淡入效果,成本最低。
最後提醒:以上範例使用 retain/release 與 UIViewController 的 view 直接操作,屬於 MRC 時代的寫法。現行專案請改用 ARC 與 weak 屬性,並以 childViewControllers 加上 addChildViewController: 的標準容器模式來管理子控制器。