iOS ChildViewController與View

來源:互聯網
上載者:User

標籤:

一、概述

  在iOS中,ViewController與View是配對使用的,一個ViewController可以對應多個View,就是指View的父控制器。當然,一個ViewController也可以對應一個View,在View1中,添加另一個View1_1,使用addSubView方法,此時,也要在View1的控制器中,添加對應View1_1的ViewController控制器,使用addChildViewController方法。

 

二、使用

1. FirstViewController.m

 1 #import "FirstViewController.h" 2 #import "SecondViewController.h" 3  4 static NSString * const FirstReuseIdentifierCell = @"FirstIdentifierCell"; 5  6 @interface FirstViewController () 7 { 8   UITableView *iTableView; 9 }10 11 @property (nonatomic, strong) UITableView *iTableView;12 13 @end14 15 @implementation FirstViewController16 17 @synthesize iTableView;18 19 - (void)viewDidLoad20 {21   [super viewDidLoad];22   CGRect frame = self.view.frame;23   frame.origin.y = 20;24   frame.size.height = CGRectGetHeight([UIScreen mainScreen].bounds) - 20;25   self.iTableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];26   self.iTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;27   self.iTableView.backgroundColor = [UIColor lightGrayColor];28   self.iTableView.delegate = self;29   self.iTableView.dataSource = self;30   31   [self.view addSubview:self.iTableView];32 }33 34 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section35 {36   return 20.0f;37 }38 39 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section40 {41   return @"First View Controller";42 }43 44 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath45 {46   return 120.0f;47 }48 49 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView50 {51   return 1;52 }53 54 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section55 {56   return 3;57 }58 59 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath60 {61   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstReuseIdentifierCell];62   if (!cell)63   {64     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstReuseIdentifierCell];65   }66   67   SecondViewController *secondViewController = [[SecondViewController alloc] init];68   CGRect secondFrame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 90);69   secondViewController.view.frame = secondFrame;70   [self addChildViewController:secondViewController];71   [cell.contentView addSubview:secondViewController.view];72   73   return cell;74 }75 76 - (void)didReceiveMemoryWarning77 {78   [super didReceiveMemoryWarning];79 }80 81 @end

 

2. SecondViewController.m

 1 #import "SecondViewController.h" 2  3 static NSString * const SecondReuseIdentifier = @"SecondReuseIdentifierCell"; 4  5 @interface SecondViewController () 6 { 7   UITableView *iTableView; 8 } 9 10 @property (nonatomic, strong) UITableView *iTableView;11 12 @end13 14 @implementation SecondViewController15 16 @synthesize iTableView;17 18 - (void)viewDidLoad19 {20   [super viewDidLoad];21   CGRect frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 90);22   self.iTableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];23   self.iTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;24   self.iTableView.separatorColor = [UIColor lightGrayColor];25   self.iTableView.backgroundColor = [UIColor lightGrayColor];26   self.iTableView.delegate = self;27   self.iTableView.dataSource = self;28   29   [self.view addSubview:self.iTableView];30 }31 32 //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section33 //{34 //  return 20.0f;35 //}36 37 //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section38 //{39 //  return @"Second View Controller";40 //}41 42 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath43 {44   return 30.0f;45 }46 47 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView48 {49   return 1;50 }51 52 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section53 {54   return 3;55 }56 57 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath58 {59   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SecondReuseIdentifier];60   if (!cell)61   {62     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:SecondReuseIdentifier];63   }64   cell.backgroundColor = [UIColor redColor];65   cell.textLabel.text = SecondReuseIdentifier;66   return cell;67 }68 69 - (void)didReceiveMemoryWarning70 {71   [super didReceiveMemoryWarning];72 }73 74 75 76 @end

 

iOS ChildViewController與View

聯繫我們

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