xcode6.1開發環境下開發視圖間切換程式,xcode6.1視圖

來源:互聯網
上載者:User

xcode6.1開發環境下開發視圖間切換程式,xcode6.1視圖

xcode6.1開發環境下開發視圖間切換程式


1、  建立一個空項目

因為xcode6.1沒有空項目,所以先從Single View Application 模板建立ios項目。

Product Name輸入Switch,完成。下面開始刪除一些檔案,並改變一些配置。

 

2、  刪除視圖和介面檔案

刪除Main.storyboard和LaunchScreen.xib ,完成後在info.plist檔案中也需要刪除對應的配置,通過點擊減號即可。

刪除預設的視圖控制器檔案ViewController.h ViewController.m;

 

 

3、  建立視圖控制器

下一步輸入控制器類名稱;

完成後產生兩個檔案:SwitchViewController.h和SwitchViewController.m;

按照同樣的步驟,建立BlueViewController.m和YellowViewController.m及對應的.h檔案。

4、  建立視圖檔案

通過建立檔案,選擇User Interface類別的View;分別命名為SwitchView、BlueView、YellowView;

 

5、  添加視圖控制器

把視圖預設的類NSObject修改成對應的控制器類;

 

6、  相關性檢視與控制器

 

同樣方式給其他兩個視圖添加控制器;

 

 

7、  修改應用程式委託

#import <UIKit/UIKit.h>

@classSwitchViewController;

@interfaceAppDelegate :UIResponder <UIApplicationDelegate>

@property (strong,nonatomic) UIWindow *window;

@property (strong,nonatomic) SwitchViewController *switchViewController;

@end

 

AppDelegate.m檔案的didFinishLaunchingWithOptions方法修改如下:

 

#import "AppDelegate.h"

#import "SwitchViewController.h"

@implementationAppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

self.window = [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];

self.switchViewController = [[SwitchViewControlleralloc]initWithNibName:@"SwitchView"bundle:nil];

    UIView *switchView =self.switchViewController.view;

    CGRect switchViewFrame = switchView.frame;

    switchViewFrame.origin.y += [UIApplicationsharedApplication].statusBarFrame.size.height;

    switchView.frame = switchViewFrame;

    self.window.rootViewController = self.switchViewController;

   

    self.window.backgroundColor = [UIColorgreenColor];

    [self.windowmakeKeyAndVisible];

   

    returnYES;

}

8、  運行測試,查看結果

綠色介面是switchView的背景顏色,並添加了Toolbar到底部。

 

9、  主控制器中申明其他控制器

#import <UIKit/UIKit.h>

@classBlueViewController;

@classYellowViewController;

 

@interfaceSwitchViewController :UIViewController

@property (strong,nonatomic)BlueViewController*blueViewController;

@property (strong,nonatomic)YellowViewController *yellowViewController;

@end

 

產生- (IBAction)switchView:(UIBarButtonItem *)sender{}方法;

 

10、              根視圖載入時插入藍色視圖

 

- (void)viewDidLoad {

    [superviewDidLoad];

    self.blueViewController = [[BlueViewControlleralloc]initWithNibName:@"BlueView"bundle:nil];

    [self.viewinsertSubview:self.blueViewController.viewatIndex:0];

}

 

11、              響應按鈕事件,實現切換

 

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    if (self.blueViewController.view.superview == nil) {

        self.blueViewController =nil;

    }else{

        self.yellowViewController =nil;

    }

}

 

- (IBAction)switchView:(UIBarButtonItem *)sender{

    if (self.yellowViewController.view.superview == nil) {

        if (self.yellowViewController.view == nil) {

            self.yellowViewController = [[YellowViewControlleralloc]initWithNibName:@"YellowView"bundle:nil];

        }

        [self.blueViewController.viewremoveFromSuperview];

        [self.viewinsertSubview:self.yellowViewController.viewatIndex:0];

    }else{

        if (self.blueViewController.view == nil) {

            self.blueViewController = [[BlueViewControlleralloc]initWithNibName:@"BlueView"bundle:nil];

        }

        [self.yellowViewController.viewremoveFromSuperview];

        [self.viewinsertSubview:self.blueViewController.viewatIndex:0];

    }

   

}

 

12、              實現動畫切換

 

- (IBAction)switchView:(UIBarButtonItem *)sender{

    [UIViewbeginAnimations:@"View Flip"context:nil];

    [UIViewsetAnimationDuration:1.25];

    [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

   

    if (self.yellowViewController.view.superview == nil) {

        if (self.yellowViewController.view == nil) {

            self.yellowViewController = [[YellowViewControlleralloc]initWithNibName:@"YellowView"bundle:nil];

        }

       

        [UIViewsetAnimationTransition:UIViewAnimationTransitionFlipFromRightforView:self.viewcache:YES];

       

        [self.blueViewController.viewremoveFromSuperview];

        [self.viewinsertSubview:self.yellowViewController.viewatIndex:0];

    }else{

        if (self.blueViewController.view == nil) {

            self.blueViewController = [[BlueViewControlleralloc]initWithNibName:@"BlueView"bundle:nil];

        }

       

        [UIViewsetAnimationTransition:UIViewAnimationTransitionFlipFromRightforView:self.viewcache:YES];

       

        [self.yellowViewController.viewremoveFromSuperview];

        [self.viewinsertSubview:self.blueViewController.viewatIndex:0];

    }

   

    [UIViewcommitAnimations];

}

 

13、              常見問題

問題1:

The file “Info.plist” couldn’t be openedbecause there is no such file

刪除test檔案沒有刪除乾淨;通過工程配置介面可以看到並刪除。

 

問題2:

'Could not load NIB in bundle:'NSBundle </Users/c/Library/Developer/CoreSimulator/Devices/C817BF05-3462-41CB-9D9D-61CB794C78D4/data/Containers/Bundle/Application/F9EC13C2-7589-41CF-A010-48958E902262/Switch.app>(loaded)' with name 'SwitchViews''

self.switchViewController = [[SwitchViewControlleralloc]initWithNibName:@"SwitchView"bundle:nil];

初始化視圖控制器是名稱沒有指定明確,區別大小寫;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.