Embed Segue(嵌入式Segue)Demo
来源:互联网 发布:叙利亚老虎师 知乎 编辑:程序博客网 时间:2024/06/10 03:39
原创Blog,转载请注明出处
blog.csdn.net/hello_hwc
首先先看看Demo的效果
在一个ViewController中,嵌入一个TableViewController,然后这个TableViewController又在UINavigationController中。
Textfield和Add button在ViewController中,点击button,把textfield中的内容添加到嵌入式的tableViewController
一 Embed Segue简介
简而言之,就是一个ViewController的View嵌入到另外一个ViewController中,也称作childViewController。这样的设计是一块区域有一个单独的ViewController,从MVC的设计模式来看,这是一个解耦合的过程,由嵌入式的ViewController来负负责一块区域的Modol和View的协调,如果由一个ViewController来实现会造成单个ViewController过于臃肿。
二 Demo 的实现过程
原理很简单,在父ViewController中保存一个子ViewController的引用,这个指针为weak(防止参与到子ViewController的ARC计数),然后在PrepareForSegue中对这个引用初始化。在add button的target action中,调用子ViewController的接口.(这里暂不考虑用何种方式传值最优,不想把问题搞复杂了)
(1)创建一个基于单页面的工程
(2)在StoryBoard中拖入一个ContainView
然后,删掉默认的ViewController,拖入一个UITableViewController
接着沿着ContainView进行control+drag拖到tableviewController中,选择Embed。
然后,为Segue设置identifier。
其他部分比较简单,就是简单的拖拽,不做讲解。
(3)核心部分代码
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([[segue identifier] isEqualToString:@"EmbedSegue"]) { if ([segue.destinationViewController isKindOfClass:[EmbedTableViewController class]]) { self.embedVC = (EmbedTableViewController *)segue.destinationViewController; } if ([segue.destinationViewController isKindOfClass:[UINavigationController class]]) { UINavigationController * nav = (UINavigationController *)segue.destinationViewController; self.embedVC = (EmbedTableViewController*)[nav.viewControllers firstObject]; } }}- (IBAction)add:(id)sender { if (self.textfield.text.length > 0) { [self.embedVC addString:self.textfield.text]; self.textfield.text = nil; }}在PreparedForSegue中,保存一个embed Segue的饮用。
注意,这里要判断有没有UINavigationController
然后,button的action中,调用embed Segue的接口来添加新的String
(4)tableView部分。
用NSMutableArray存储Model
然后在
addString接口中在最后一行插入
static NSString * identifer = @"cell";@interface EmbedTableViewController()<UITableViewDataSource,UITableViewDelegate>@property (strong,nonatomic) NSMutableArray * array;@end@implementation EmbedTableViewController#pragma mark - Property-(NSMutableArray *)array{ if (_array == nil) { _array = [[NSMutableArray alloc] initWithObjects:@"initalFirst",@"initalSecond", nil]; } return _array;}#pragma mark - API-(void)addString:(NSString *)text{ [self.array addObject:text]; [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.array.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft]; [self.tableView endUpdates];}#pragma mark - tableViewDataSource-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.array.count;}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer forIndexPath:indexPath]; NSString * text = [self.array objectAtIndex:indexPath.row]; cell.textLabel.text = text; return cell;}-(void)viewDidLoad{ [super viewDidLoad]; self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];}完整的Demo下载
CSDN下载
http://download.csdn.net/detail/hello_hwc/8365813
- Embed Segue(嵌入式Segue)Demo
- SEGUE
- Segue
- Borland (Segue)
- iOS segue
- Segue对象
- Segue跳转
- 自定义segue
- segue跳转
- ios-Segue
- IOS Modal Segue和Unwind Segue的使用Demo-附完整工程
- 灵活使用segue导航
- iOS: Segue & Delegate
- scene, segue, relationship
- storyboard segue 传递数据
- IOS学习笔记---segue
- storyboard中的segue
- 通过segue进行数据传输
- Roman to Integer leetcode
- 数据库问题
- android开发之应用程序如何完全退出
- 20150115 瑞士央行取消汇率下限,瑞郎全线疯狂暴涨20% 黄金1260
- php设计模式——装饰器模式
- Embed Segue(嵌入式Segue)Demo
- Linux socket 及多线程 实例
- 当一个.NET托管程序运行时都发生了什么,关闭时发生了什么
- Matplotlib 基础
- 旋转转盘 算法
- Excel Sheet Column Number
- Python+Hadoop Streaming实现MapReduce(如何给map和reduce的脚本传递参数)
- 详细阐述Web开发中的图片上传问题
- JAVA 应用 V - 方法的可变参数。 format : typeName... parameters