您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 用一顆按鈕切換畫面背景:兩種視圖切換架構實作

編輯:iOS 開發綜合

「按一下按鈕換背景顏色」聽起來是新手練習題,但它背後其實藏著一個重要的觀念差異:用 rootViewController 管理子控制器,還是直接把控制器的 view 疊在 window 上。這兩種做法在記憶體管理、狀態保存與返回行為上完全不同。

做法一:以 RootViewController 作為容器

建立一個 root 控制器,內部持有兩個子控制器(紅底與黃底),透過 insertSubview / removeFromSuperview 做切換。這是較正規的寫法。

RootViewController 的宣告

#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 中記得釋放兩個子控制器。

AppDelegate 掛上 root

HHLrootViewController *rootView1 =
    [[HHLrootViewController alloc] initWithNibName:nil bundle:nil];
self.rootView = rootView1;
[rootView1 release];
self.window.rootViewController = self.rootView;
[self.window makeKeyAndVisible];

做法二:直接把兩個 view 疊在 window 上

另一種思路是乾脆不用 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];

兩種做法該怎麼選

  • 做法一(rootViewController):多數情況的正確答案。子控制器的檢視週期(viewWillAppear: 等)會被正常觸發,適合每個畫面有獨立生命週期邏輯的場景。
  • 做法二(疊在 window):程式碼最少,但兩個 view 同時存在且同時佔用記憶體,而且子控制器的視圖控制回呼不會被呼叫。只適合極簡單的切換,不建議用於正式專案。

另外,若只是要「換背景顏色」而非換整個畫面,其實根本不需要兩個 view controller——直接對 self.view.backgroundColor 做動畫切換即可,="#[UIColor colorWithRed:...]" 搭配 UIView 動畫就能做出淡入效果,成本最低。

最後提醒:以上範例使用 retain/release 與 UIViewController 的 view 直接操作,屬於 MRC 時代的寫法。現行專案請改用 ARC 與 weak 屬性,並以 childViewControllers 加上 addChildViewController: 的標準容器模式來管理子控制器。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved