你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS頁面之間切換

iOS頁面之間切換

編輯:IOS開發綜合

打開ContainerViewController.h為:


#import


@interface ContainerViewController :UIViewController


//[dubai]切換兩個頁面


- (void)exchangeFirstViewAndSecondView;




@end


打開ContainerViewController.m為:

#import "ContainerViewController.h"


#import "FirstViewController.h"

#import "SecondViewController.h"




@interface ContainerViewController ()

{

FirstViewController *_firstVC;

SecondViewController *_secondVC;




}

@end


@implementation ContainerViewController


- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view.

self.title =@"切換頁面";

//創建firstVC

_firstVC = [[FirstViewControlleralloc]init];

[selfaddChildViewController:_firstVC];

//創建seconfVC

_secondVC = [[SecondViewControlleralloc]init];

[selfaddChildViewController:_secondVC];

[self.viewaddSubview:_firstVC.view];

}



//[dubai]切換兩個頁面


- (void)exchangeFirstViewAndSecondView

{

//沒有父視圖,並沒有顯示(但是有 ,對象存在)

if (_firstVC.view.superview !=nil){

//從第一個頁面,進入第二個頁面(移除firstView添加secondView)

[_firstVC.viewremoveFromSuperview];

[self.viewaddSubview:_secondVC.view];

}else{

//從第二個頁面進入第一個頁面

[_secondVC.viewremoveFromSuperview];

[self.viewaddSubview:_firstVC.view];

}

}


打開FirstViewController.m為:

//

// FirstViewController.m

// ChangeControllerDemo

//

// Created by Dubai on 14/10/4.

// Copyright (c) 2015年 Dubai. All rights reserved.

//


#import "FirstViewController.h"


#import "ContainerViewController.h"




@interface FirstViewController ()


@end


@implementation FirstViewController


- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColorredColor];

UIButton *toSecondBtn = [UIButtonbuttonWithType:(UIButtonTypeSystem)];

toSecondBtn.frame =CGRectMake(60,100,100, 40);

[toSecondBtn setTitle:@"前往第二頁"forState:(UIControlStateNormal)];

[toSecondBtn addTarget:selfaction:@selector(clickToSecongBtnAction:)forControlEvents:(UIControlEventTouchUpInside)];

[self.viewaddSubview:toSecondBtn];


UILabel *lable = [[UILabelalloc]initWithFrame:CGRectMake(100,200,100, 40)];

lable.text =@"第一頁";

[self.viewaddSubview:lable];

}


//[Dubai]前往第二頁按鈕的響應方法

- (void)clickToSecongBtnAction:(UIButton *)btn


{


//contrainterVC切換顯示切換兩個視圖 強轉對象類型

ContainerViewController *containterVC = (ContainerViewController *)self.parentViewController;

[containterVC exchangeFirstViewAndSecondView];



}


- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

if ([self isViewLoaded] &&self.view.window ==nil) {

self.view =nil;

}

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/


@end




打開SecondViewController.m為:

//

// SecondViewController.m

// ChangeControllerDemo

//

// Created by Dubai on 14/10/4.

// Copyright (c) 2015年 Dubai. All rights reserved.

//


#import "SecondViewController.h"

#import "ContainerViewController.h"



@interface SecondViewController ()


@end


@implementation SecondViewController


- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColorcyanColor];

//創建button前往第一頁

UIButton *toFirstButton = [UIButtonbuttonWithType:(UIButtonTypeSystem)];

toFirstButton.frame =CGRectMake(100,100,100, 40);

[toFirstButton setTitle:@"前往第一頁"forState:(UIControlStateNormal)];

[self.viewaddSubview:toFirstButton];

[toFirstButtonaddTarget:selfaction:@selector(didClickToFirstButtonAction:)forControlEvents:(UIControlEventTouchUpInside)];

UILabel *lable = [[UILabelalloc]initWithFrame:CGRectMake(100,200,100, 40)];

lable.text =@"第二頁";

[self.viewaddSubview:lable];


}


//[Dubai]前往第一個按鈕的方法

- (void)didClickToFirstButtonAction:(UIButton *)button

{

//contrainterVC切換顯示切換兩個視圖 強轉對象類型

ContainerViewController *containterVC = (ContainerViewController *)self.parentViewController;

[containterVC exchangeFirstViewAndSecondView];

}



- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/


@end


圖為: \


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