貓貓學iOS 之控制器view顯示中view的父子關係及controller的父子關係_解決旋轉螢幕不能傳遞事件問題

來源:互聯網
上載者:User

貓貓學iOS 之控制器view顯示中view的父子關係及controller的父子關係_解決旋轉螢幕不能傳遞事件問題

 

一:效果

二:項目代碼

這個Demo用的幾個控制器分別畫了不通的xib,隨便拖拽了幾個空間,主要是幾個按鈕的切換,主要代碼展示下:

////  NYViewController.m//  控制器的view的顯示////  Created by apple on 14-10-10.//  Copyright (c) 2014年 heima. All rights reserved.//#import NYViewController.h#import NYTestViewController.h#import NYOneViewController.h#import NYTwoViewController.h#import NYThreeViewController.h@interface NYViewController ()- (IBAction)vc1;- (IBAction)vc2;- (IBAction)vc3;@property (nonatomic, strong) NYTestViewController *test;@property (nonatomic, strong) NYOneViewController *one;@property (nonatomic, strong) NYTwoViewController *two;@property (nonatomic, strong) NYThreeViewController *three;@end@implementation NYViewController- (NYOneViewController *)one{    if (!_one) {        self.one = [[NYOneViewController alloc] init];        self.one.view.frame = CGRectMake(10, 70, 300, 300);    }    return _one;}- (NYTwoViewController *)two{    if (!_two) {        self.two = [[NYTwoViewController alloc] init];        self.two.view.frame = CGRectMake(10, 70, 300, 300);    }    return _two;}- (NYThreeViewController *)three{    if (!_three) {        self.three = [[NYThreeViewController alloc] init];        self.three.view.frame = CGRectMake(10, 70, 300, 300);    }    return _three;}/** * 即將旋轉到某個螢幕時調用 */- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{    NSLog(@NYViewController---willRotateToInterfaceOrientation);}- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{    NSLog(@NYViewController---didRotateFromInterfaceOrientation);}- (void)viewDidLoad{    [super viewDidLoad];//    NYTestViewController *test = [[NYTestViewController alloc] init];//    test.view.frame = CGRectMake(100, 100, 200, 300);//    test.view.backgroundColor = [UIColor redColor];//    [self.view addSubview:test.view];//    self.test = test;    // 如果發現:控制器的view還在,但是view上面的資料不顯示,極大可能是因為:控制器被提前銷毀了    // 1.一個控制器的view是可以隨意調整尺寸和位置的    // 2.一個控制器的view是可以隨意添加到其他view中    // 3.如果將一個控制器的view,添加到其他view中顯示,那麼要想辦法保證控制器不被銷毀    // 4.原則:只要view在,view所在的控制器必須得在,這樣才能保證view內部的資料和商務邏輯正常}- (IBAction)vc1 {    [self.two.view removeFromSuperview];    [self.three.view removeFromSuperview];    [self.view addSubview:self.one.view];}- (IBAction)vc2 {    [self.one.view removeFromSuperview];    [self.three.view removeFromSuperview];    [self.view addSubview:self.two.view];}- (IBAction)vc3 {    [self.two.view removeFromSuperview];    [self.one.view removeFromSuperview];    [self.view addSubview:self.three.view];}@end
三:旋轉事件問題

這樣貌似就可以完成大多數的需求了,但是有時候我們會發現一些問題,比如當旋轉螢幕的時候事件無法傳遞

/** * 即將旋轉到某個螢幕時調用 */- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{    NSLog(@NYViewController---willRotateToInterfaceOrientation);}- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{    NSLog(@NYViewController---didRotateFromInterfaceOrientation);}

如果我們將這兩個方法寫到one two three這三個控制器中,相應的在旋轉螢幕的時候,只有主控制器列印了這個方法,然而其他的控制器中並沒有,這裡的原因就是他們的控制器是平級的,雖然view是父子關係,解決辦法就是設定controller的父子關係。

四:解決代碼

當控制器的view互為父子關係,那麼控制器最好也互為父子關係

  NYOneViewController *one = [[NYOneViewController alloc]init];

讓one控制器成為當前self(HWViewController)的子控制器

    [self addChildViewController:one];

通過關addChildViewController添加一個子控制器,那麼這個控制器就會被放到childViewControllers數組中
只要self在,childViewControllers數組就在數組裡面的子控制器就在

////  NYViewController.m//  控制器的view的顯示////  Created by apple on 14-10-10.//  Copyright (c) 2014年 heima. All rights reserved.//#import NYViewController.h#import NYTestViewController.h#import NYOneViewController.h#import NYTwoViewController.h#import NYThreeViewController.h@interface NYViewController ()- (IBAction)vc1;- (IBAction)vc2;- (IBAction)vc3;@property (nonatomic, strong) NYTestViewController *test;@end@implementation NYViewController/** * 即將旋轉到某個螢幕時調用 */- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{    NSLog(@NYViewController---willRotateToInterfaceOrientation);}- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{    NSLog(@NYViewController---didRotateFromInterfaceOrientation);}- (void)viewDidLoad{    [super viewDidLoad];    //當控制器的view互為父子關係,那麼控制器最好也互為父子關係    NYOneViewController *one = [[NYOneViewController alloc]init];    //讓one控制器成為當前self(HWViewController)的子控制器    [self addChildViewController:one];    //通過關addChildViewController添加一個子控制器,那麼這個控制器就會被放到childViewControllers數組中    //只要self在,childViewControllers數組就在數組裡面的子控制器就在    NYTwoViewController *two = [[NYTwoViewController alloc]init];    [self addChildViewController:two];    NYThreeViewController *three = [[NYThreeViewController alloc]init];    [self addChildViewController:three];}- (IBAction)vc1 {    NYOneViewController *one = self.childViewControllers[0];    NYTwoViewController *two = self.childViewControllers[1];    NYThreeViewController *three = self.childViewControllers[2];    [two.view removeFromSuperview];    [three.view removeFromSuperview];    [self.view addSubview:one.view];}- (IBAction)vc2 {    NYOneViewController *one = self.childViewControllers[0];    NYTwoViewController *two = self.childViewControllers[1];    NYThreeViewController *three = self.childViewControllers[2];    [one.view removeFromSuperview];    [three.view removeFromSuperview];    [self.view addSubview:two.view];}- (IBAction)vc3 {    NYOneViewController *one = self.childViewControllers[0];    NYTwoViewController *two = self.childViewControllers[1];    NYThreeViewController *three = self.childViewControllers[2];    [two.view removeFromSuperview];    [one.view removeFromSuperview];    [self.view addSubview:three.view];}@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.