iOS中表视图单元格事件用nib和storyboard的两种写法总结

从ios6开始,苹果公司推出了storyborad技术取代了nib的写法,这样代码量确实少写了很多,也比较简洁。但是,从学习的角度来说,阿堂认为 用nib的写法,虽然多了些代码,但是对于掌握知识和原理的角度来说,我认为nib写法也挺不错的。用storyborad的写法时,如果segue场景 较多的话,设置有问题的话,会导致一些异常的发生,增加调试的难度。下面阿堂亲自了测试了nib和storyboard的两种写法的demo。下面将其差 异之处简单对比了下,供有需掌握的网友了解下。

 

demo效果图如下
       
iOS中表视图单元格事件用nib和storyboard的两种写法总结_第1张图片

iOS中表视图单元格事件用nib和storyboard的两种写法总结_第2张图片

iOS中表视图单元格事件用nib和storyboard的两种写法总结_第3张图片


iOS中表视图单元格事件用nib和storyboard的两种写法总结_第4张图片


       对于表视图的单元cell选中时,两种写法是不一样的。下面是阿堂分别取自于 ViewController.m和 CitiesViewController.m方中的写法

一.代码的写法比较
 
ViewController.m

nib写法
#pragma mark - 实现表视图委托方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
   
    CitiesViewController *citiesViewController = [[CitiesViewController alloc] initWithStyle:UITableViewStylePlain];
    NSString *selectName = [self.listData objectAtIndex:row];
    citiesViewController.listData = [self.dictData objectForKey:selectName];
    citiesViewController.title = selectName;
    [self.navigationController pushViewController:citiesViewController animated:YES];

}

storyboard写法
//选择表视图行时候触发
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   
    if([segue.identifier isEqualToString:@"ShowSelectedProvince"])
    {
        CitiesViewController *citiesViewController = segue.destinationViewController;
        NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
        NSString *selectName = [self.listData objectAtIndex:selectedIndex];
        citiesViewController.listData = [self.dictData objectForKey:selectName];
        citiesViewController.title = selectName;
    }
}

CitiesViewController.m
nib写法
#pragma mark - 实现表视图委托方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
   
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
   
    NSDictionary *dict = [self.listData objectAtIndex:row];
   
    detailViewController.url = [dict objectForKey:@"url"];
    NSString *name = [dict objectForKey:@"name"];
    detailViewController.title = name;
    [self.navigationController pushViewController:detailViewController animated:YES];
   
}
storyboard写法
//选择表视图行时候触发
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
     
    if([segue.identifier isEqualToString:@"ShowSelectedCity"])
    {
        DetailViewController *detailViewController = segue.destinationViewController;
       
        NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
        NSDictionary *dict = [self.listData objectAtIndex:selectedIndex];
       
        detailViewController.url = [dict objectForKey:@"url"];
       
        NSString *name = [dict objectForKey:@"name"];
        detailViewController.title = name;
    }
}

二. 操作方式上
storyboard的 导航浏览器,不需要代码定义,直接选中某viewcontroller ,再选中菜单操作
Editor-->Embed in--->Navigation Controller

从 一个viewcontroller 导航到另一个viewcontroller时,直接在界面 push就可以了 ,push时,需要选中 连线中间的segue,打开其中的属生检查器,然后在identifier属性中指明,如 ShowSelectedProvinces



iOS中表视图单元格事件用nib和storyboard的两种写法总结_第5张图片

你可能感兴趣的:(ios)