頁面切換教程 使用了ARC技術

來源:互聯網
上載者:User

下載資源:

http://download.csdn.net/detail/likandmydeer/5377303


第一步:建立project,選擇Single View Application

 

然後點next,進入如:

把Use Automatic Reference Counting 選上。再點擊next,

選擇好儲存的路徑,再點擊create

 

 

第二步:

建立兩個類,並命名為YellowViewController , BlueViewController。

選上With XIB for user interface,再點Next。

 

 

 

 

第三步;點擊YellowViewController.xib

 

 

點擊YellowViewController.xib 下的View,然後在右邊的屬性欄選擇,然後改變背景顏色,改為黃色

第四步:重複第三步,改變BlueViewController背景顏色為藍色:

 

第五步:

拉一個toolbar到 ViewController.xib 裡面:

然後改變Bar Button Item 的名字,改為“跳轉”。

 

第六步:點擊右上方的Show the Assistanteditor

 

如,

 

 

按著control 從按鈕一直指ViewController.h 裡面在@interface 和@end之間,如;

 

第七步:點擊ViewController.h開始編寫代碼。

 

代碼如下:

////  ViewController.h//  MySwitchViewTest////  Created by apple on 13-5-14.//  Copyright (c) 2013年 ___FULLUSERNAME___. All rights reserved.//#import <UIKit/UIKit.h>@class BlueViewController;@class YellowViewController;@interface ViewController : UIViewController{    BlueViewController* myBlueViewController;    YellowViewController* myYellowViewController;}@property ( strong , nonatomic ) BlueViewController* myBlueViewController;@property ( strong , nonatomic ) YellowViewController* myYellowViewController;- (IBAction)ClickMe:(id)sender;@end

再到ViewController.m檔案編寫代碼:

 

////  ViewController.m//  MySwitchViewTest////  Created by apple on 13-5-14.//  Copyright (c) 2013年 ___FULLUSERNAME___. All rights reserved.//#import "ViewController.h"#import "BlueViewController.h"#import "YellowViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize myBlueViewController;@synthesize myYellowViewController;- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.        // 建立 blueController的執行個體    BlueViewController* blueController = [ [ BlueViewController alloc ] init ];    //    NSLog(  );        // 把 blueController 賦值給這個類的成員 myBlueViewController    self.myBlueViewController = blueController;        // 把 blueController.view 插入到這個視窗的第0位置上    [ self.view insertSubview: blueController.view atIndex: 0 ];    }- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.        // 記憶體處理 , 釋放表單記憶體    if ( self.myBlueViewController.view.superview == nil )    {        self.myBlueViewController = nil;    }    else        self.myYellowViewController = nil;    }- (IBAction)ClickMe:(id)sender{    if ( self.myYellowViewController.view.superview == nil )    {        if ( self.myYellowViewController == nil )        {            // 建立 yellowController的執行個體            YellowViewController* yellowController = [ [ YellowViewController alloc ] init ];                        // 把 yellowController 賦值給這個類的成員 myYellowViewController            self.myYellowViewController = yellowController;        }                // 把 myBlueViewController.view 移除        [ myBlueViewController.view removeFromSuperview ];                // 把 yellowController.view 插入到這個視窗的第0位置上        [ self.view insertSubview: myYellowViewController.view atIndex: 0 ];            }    else    {        if ( self.myBlueViewController == nil )        {                        // 建立 blueController的執行個體            BlueViewController* blueController = [ [ BlueViewController alloc ] init ];                        // 把 blueController 賦值給這個類的成員 myBlueViewController            self.myBlueViewController = blueController;        }                // 把 myYellowViewController.view 移除        [ myYellowViewController.view removeFromSuperview ];                // 把 blueController.view 插入到這個視窗的第0位置上        [ self.view insertSubview: myBlueViewController.view atIndex: 0 ];    }    }@end

完成了!簡單吧!


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.