storyBoard学习总结

来源:互联网 发布:无冬之夜mac 编辑:程序博客网 时间:2024/06/05 19:36

一:nsrange的用法,可以通过

    if (urgentRange.location == NSNotFound)来判断是否找到了子串


    NSString *identifier =nil;

    NSString *task = [self.tasksobjectAtIndex:indexPath.row];

    NSRange urgentRange = [task rangeOfString:@"URGENT"];

    if (urgentRange.location == NSNotFound) {

        identifier =@"plainCell";

    }else{

        identifier =@"attentionCell";

    }


二:storyboard学习

1,以前界面跳转的时候,使用代理函数,传递数据也在此函数

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

使用storyboard之后,代理函数变为

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

//视图跳转的时候,传数据给下一个视图控制器

//以前我们使用tableViewdidSelectRowAtIndexPath:方法,选择某行的时候执行的代理函数,来进行视图的跳转与传递数据.现在使用下面的方法 

//当一个segue被激活时,将会使当前的控制器视图被另一个控制器视图替代,此时就调用下面这个方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    //segue.destinationViewController就是即将要显示的视图VC,另外可以使用self.sourceViewController表示即将从屏幕上面移去的视图VC

    UIViewController *destination = segue.destinationViewController;

    if ([destination respondsToSelector:@selector(setDelegate:)]) {

        [destinationsetValue:selfforKey:@"delegate"];//设置当前视图控制器taskVC为taskDetailVC的代理

    }

    if ([destination respondsToSelector:@selector(setSelection:)]) {

        //prepare selection info

        NSIndexPath *indexPath = [self.tableViewindexPathForCell:sender];

        id object = [self.tasksobjectAtIndex:indexPath.row];

        NSDictionary *selection = [NSDictionarydictionaryWithObjectsAndKeys:indexPath,@"indexPath",object,@"object",nil];

        [destinationsetValue:selectionforKey:@"selection"];//传递数据selection给即将要显示的视图

    }

}


2,在从视图返回上一级视图的时候,回传数据,使用代理,把数据传给代理视图控制器的editedSelection属性。

taskDetailVC.m文件中:

- (void)viewWillDisappear:(BOOL)animated

{

    [superviewWillDisappear:animated];

    

    if ([_delegate respondsToSelector:@selector(setEditedSelection:)]) {

        //finish editing

        [_textViewendEditing:YES];

        //prepare seletion info

        NSIndexPath *indexPath = [_selection objectForKey:@"indexPath"];

        id object = _textView.text;

        NSDictionary *editedSelection = [NSDictionarydictionaryWithObjectsAndKeys:indexPath,@"indexPath",object,@"object",nil];

        [_delegatesetValue:editedSelectionforKey:@"editedSelection"];//当前的代理就是detail视图的上一级视图taskVC,把数据传给上一级视图,taskVC视图重写了editedSelectionsetter方法,在设置值的时候会顺便更新ui,详情看如下函数:setEditedSelection

   }

}


//detail视图调用[_delegate setValue:editedSelection forKey:@"editedSelection"];的时候,setter方法就使用下面的,set的时候更新ui

- (void)setEditedSelection:(NSDictionary *)dict

{

    if (![dict isEqual:_editedSelection]) {

        _editedSelection = dict;

        NSIndexPath *indexPath = [dict objectForKey:@"indexPath"];

        id newValue = [dict objectForKey:@"object"];

        [tasksreplaceObjectAtIndex:indexPath.rowwithObject:newValue];

        [self.tableViewreloadRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];

    }

}


总结:回传数据的时候,上一级VC必须拥有下一级视图的实例,上面的例子中在taskVC中就通过

UIViewController *destination = segue.destinationViewController;获取了detailTadkVC的实例。然后设置了代理。

如果不用storyboard,那么就不能通过这个方法获得实例。

使用xib的时候,要回传数据,应该这么写:

#ifdef USE_XIB //要包含头文件,然后创建detail的实例

#import "BIDTaskDetailViewController.h" 

#endif

#ifdef USE_XIB

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    BIDTaskDetailViewController *detailVC = [[BIDTaskDetailViewControlleralloc]init];

    detailVC.delegate =self;

}

#endif

这么看起来,使用storyboard之后,根本不用创建下一级视图的实例,简单了很多。




原创粉丝点击