segue有三种跳转方式:push,model,custom
1.点击某个button后根据Identifier跳转
[self performSegueWithIdentifier:@"pushChatWindow" sender:title];
这里的title可以是id,NSString,NSObject等类型,是页面跳转传递的参数。
如果在这个跳转里需要给目的ViewController传参数,需要在下面里调用[ setValue: forKey:]
forKey:@“key”,这里的key是segue.destinationViewController
的成员变量
- (void)prepareForSegue:(UIStoryboardSegue *)segue
sender:(id)sender
{
//此时这里的sender的值就是上面传过来的title
[segue.destinationViewController setValue:sender forKey:@"chatterID"];
}
2.如果不想代码实现点击按钮 根据Identifier跳转,传参时需在在ViewController里重写
- (void)prepareForSegue:(UIStoryboardSegue *)segue
sender:(id)sender
{
NSString *name = @“123”;
[segue.destinationViewController setValue:name forKey:@“name"];
}
3.如果当前ViewController里有多个segue跳转,其中一个 segue跳转后的是一个UINavigationController的根视图,一个是model方式跳转。(没实现[self performSegueWithIdentifier:@"pushChatWindow"sender:title])
此时需要在当前的ViewController里重写
-(void)prepareForSegue:(UIStoryboardSegue *)segue
sender:(id)sender{
if ([segue.identifier isEqualToString:@"NewColor"])
{//判断是哪个跳转
//需要传的参数 color
JZColorDesModel *color
= [[JZColorDesModel alloc] init];
[self.colors addObject:color];
[self.colors addObject:color];
//通过UIStoryboardSegue对象设置JZColorViewController对象的颜色,给UINavigationController根视图传参
UINavigationController *nc
= (UINavigationController*)segue.destinationViewController;
ColorViewController *mvc = (ColorViewController *)[nc topViewController];
mvc.colorDes = color;
ColorViewController *mvc = (ColorViewController *)[nc topViewController];
mvc.colorDes = color;
// 也可以用 [mvc
setValue:color forKey:@"colorDes"]
}else if([segue.identifier isEqualToString:@"ExitingColor"]){
//点击UITableViewCell push下一个ViewController
//对于push样式的UIStoryboardSegue对象,sender是UITableViewCell对象
NSIndexPath *ip = [self.tableView indexPathForCell:sender];
JZColorDesModel *color = self.colors[ip.row];
//设置ColorViewController对象的颜色
//同时设置其exitingColor属性为YES
ColorViewController *cvc = (ColorViewController *)segue.destinationViewController;
NSIndexPath *ip = [self.tableView indexPathForCell:sender];
JZColorDesModel *color = self.colors[ip.row];
//设置ColorViewController对象的颜色
//同时设置其exitingColor属性为YES
ColorViewController *cvc = (ColorViewController *)segue.destinationViewController;
cvc.colorDes = color;
cvc.existingColor = YES
// 也可以用 [cvc
setValue:color forKey:@"colorDes"]
//color和ColorViewController里的成员变量colorDes指向同一块内存。
}
}
//根据 segue Identifier跳转界面
[self performSegueWithIdentifier:@"GotoTwo" sender:self];
//以modal 方式跳转
[self presentModalViewController:VC animated:YES];
//压进一个viewcontroller
[self.navigationController pushViewController:VC animated:YES];
//弹出一个viewcontroller 相当与返回上一个界面
[self.navigationController popViewControllerAnimated:YES];
// 以 modal跳转的返回方法
[self dismissModalViewControllerAnimated:YES];
4.storyboard里不用拖线方式来push视图
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *VC
= [sb instantiateViewControllerWithIdentifier:@“second"];
[self.navigationController pushViewController:VC animated:YES];