iOS ChildViewController与View

一、概述

  在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 @end
14
15 @implementation FirstViewController
16
17 @synthesize iTableView;
18
19 - (void)viewDidLoad
20 {
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)section
35 {
36   return 20.0f;
37 }
38
39 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
40 {
41   return @"First View Controller";
42 }
43
44 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
45 {
46   return 120.0f;
47 }
48
49 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
50 {
51   return 1;
52 }
53
54 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
55 {
56   return 3;
57 }
58
59 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
60 {
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)didReceiveMemoryWarning
77 {
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 @end
13
14 @implementation SecondViewController
15
16 @synthesize iTableView;
17
18 - (void)viewDidLoad
19 {
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)section
33 //{
34 //  return 20.0f;
35 //}
36
37 //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
38 //{
39 //  return @"Second View Controller";
40 //}
41
42 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
43 {
44   return 30.0f;
45 }
46
47 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
48 {
49   return 1;
50 }
51
52 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
53 {
54   return 3;
55 }
56
57 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
58 {
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)didReceiveMemoryWarning
70 {
71   [super didReceiveMemoryWarning];
72 }
73
74
75
76 @end
时间: 2024-08-12 14:07:44

iOS ChildViewController与View的相关文章

ios 获取当前view的controller

 //获取view的controller  - (UIViewController *)viewController {     for (UIView* next = [self superview]; next; next = next.superview) {         UIResponder *nextResponder = [next nextResponder];          if ([nextResponder isKindOfClass:[UIViewControll

用ios代码做view

import“ViewController.h" @interface ViewController() @end @implementation ViewController -(void)viewDidload { [super viewDidload]; UIView *redview=[[UIView alloc]init]; redview.frame=CGRectMake(0,0,300,100); redview.backgroundcolor=[UIColor redColor]

iOS之Container View获取ViewController

最近使用Container View来在主View Controller建立自己的子Controller,可是遇到问题,不知道如何用代码获取Controller View附带的View Controller. 本想获取了其附带的View Controller,在里面设置代理,然后再主View Controller中实现代理方法,找了很多资料,解决方案如下. 建立一个.h与.m文件,使其关联Container view附带的View Controller,在里面设置代理. 点击Segue,设置Id

ios中使view向左移动,且右边不动

ios中使view向左移动,且右边不动 by 伍雪颖 -(void)signUpAndLoginAnimation:(BOOL)showSignUp { CGFloat LogInWidthChange = (showSignUp)?-120:120; [UIView animateWithDuration:0.3f animations:^{ CGRect newFrame = self.logInView2.frame; newFrame.origin.x -= LogInWidthChan

IOS开发—通过ChildViewController实现view的切换

ChildViewController的应用 viewControlle中可以添加多个subView,在需要的时候显示出来:另一种方法是通过向parentViewController中可以添加多个childCiewController;来控制页面中的subView,降低代码耦合度:通过切换子视图控制器,可以显示不同的view:,替代之前的addSubView的管理. 本节通过类似百度新闻模块切换的界面来演示ChileViewController的应用: 文档结构: 代码演示: #import "

深入理解IOS布局和view加载显示

前言 一个控件从外在特征来说,主要是封装这几点: 交互方式 显示样式 数据使用 对外在特征的封装,能让我们在多种环境下达到 PM 对产品的要求,并且提到代码复用率,使维护工作保持在一个相对较小的范围内:而一个好的控件除了有对外一致的体验之外,还有其内在特征: 灵活性 低耦合 易拓展 易维护 通常特征之间需要做一些取舍,比如灵活性与耦合度,有时候接口越多越能适应各种环境,但是接口越少对外产生的依赖就越少,维护起来也更容易.通常一些前期看起来还不错的代码,往往也会随着时间加深慢慢“成长”,功能的增加

IOS中Table View控件练习

之前两篇博客简单学习了Picker view控件的使用,接下来再学习IOS应用中很重要的一个视图--表视图. 在表视图中,每个表视图都是UITableView的一个实例,每个可见行都是UITableViewCell的一个实例. 表视图有两种基本格式,分组的表和普通表,普通表可以实现索引,实现了索引的表成为索引表.(PS.技术上,分组表也可以实现索引,不过好像苹果的设计规范中不支持) 一个简单的表视图应用 界面设计: 向storyboard中拖一个table view控件,他会自动占满屏幕,至于约

【IOS笔记】View Controller Basics

View Controller Basics   视图控制器基础 Apps running on iOS–based devices have a limited amount of screen space for displaying content and therefore must be creative in how they present information to the user. Apps that have lots of information to display

iOS UIKit:view

1.View架构 1.1 简介 UIView表示屏幕上的一块矩形区域,它在App中占有绝对重要的地位,因为IOS中几乎所有可视化控件都是UIView的子类.UIView的功能 :         1) 管理矩形区域里的内容:         2) 处理矩形区域中的事件:         3) 子视图的管理:         4) 实现动画. 图 11 UIView及子类继承关系 1.2 基本结构体         1) CGPoint        该结构表示在二维坐标系中的坐标点. struc