Embed Segue(嵌入式Segue)Demo

原创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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值