您的位置:首页 > 产品设计 > UI/UE

【iOS】Segue的页面跳转和传值

2016-01-16 11:11 357 查看
效果图

在storyboard(故事板)中我们可以轻松的就把界面之间的跳转关系展示出来。但是在页面的跳转的时候就需要Segue了。

首先来看看效果动画。



工程结构图:



页面的跳转方式

《【iOS】一种应用登录和退出跳转逻辑的实现》 有两种页面的跳转:

pushViewController和presentViewController。

那么使用Segue怎么实现呢?

页面跳转结构:

ViewController --presentViewController--> SecondViewController --presentViewController--> ThirdViewController --pushViewController--> FourViewController。其中ThirdViewController开始是带导航栏的。
在storyboard(故事板)将ViewController和SecondViewController链接起来的时候

点击方圈中间



可以设置跳转类型和跳转动画了。



代码部分:

//
//  ViewController.m
//  Segue
//
//  Created by zhuming on 16/1/13.
//  Copyright © 2016年 zhuming. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/**
*  由ViewController跳转到SecondViewController
*
*  @param sender sender description
*/
- (IBAction)ModalBtnClick:(UIButton *)sender {
[self performSegueWithIdentifier:@"Second" sender:self];
}

@end


//
//  SecondViewController.m
//  Segue
//
//  Created by zhuming on 16/1/13.
//  Copyright © 2016年 zhuming. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/**
*  由SecondViewController返回到ViewController
*
*  @param sender sender description
*/
- (IBAction)backbtnClick:(UIButton *)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
/**
*  由SecondViewController跳转到ThirdViewController
*
*  @param sender sender description
*/
- (IBAction)nextbtnClick:(UIButton *)sender {
[self performSegueWithIdentifier:@"Third" sender:self];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end


//
//  ThirdViewController.m
//  Segue
//
//  Created by zhuming on 16/1/13.
//  Copyright © 2016年 zhuming. All rights reserved.
//

#import "ThirdViewController.h"
#import "FourViewController.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/**
*  由ThirdViewController返回到SecondViewController
*
*  @param sender sender description
*/
- (IBAction)backBtnCLick:(UIButton *)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
/**
*  由ThirdViewController跳转(Push)到FourViewController
*
*  @param sender sender description
*/
- (IBAction)nextBtnCLick:(UIButton *)sender {
// 使用这个会跳转两次
//    [self performSegueWithIdentifier:@"Four" sender:self];

//    FourViewController *fourVC = [[FourViewController alloc] init];
// 这里给nil就会报警告
[self.navigationController pushViewController:nil animated:YES];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
/**
*  这个方法在页面跳转动作之前执行
*  页面传值  可以在这个里面操作
*  @param segue  segue description
*  @param sender sender description
*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"Four"]) {
FourViewController *fourVC = segue.destinationViewController;
fourVC.navTitle = @"传值";
}
}

@end


//
//  FourViewController.m
//  Segue
//
//  Created by zhuming on 16/1/13.
//  Copyright © 2016年 zhuming. All rights reserved.
//

#import "FourViewController.h"

@interface FourViewController ()
@property (weak, nonatomic) IBOutlet UILabel *textLabel;

@end

@implementation FourViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.textLabel.text = self.navTitle;
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/**
*  由FourViewController返回ThirdViewController
*
*  @param sender sender description
*/
- (IBAction)backBtnClick:(UIButton *)sender {
[self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end


代码里面都有注释了。下载请点击我
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: