1 前言
上一次我們介紹了Storyboard的簡單創建方法,這次我們來介紹一下如何通過Storyboard進行兩個ViewController之間的傳值。
2 代碼實例
2.1 新建一個不帶nib文件ViewController文件
2.2 將Storyboard中的第二個視圖控制器的類設置為新建的SecondView
2 .3 為視圖過度的Segue標識設置一個名稱
ZYViewController.m
[plain]
//通知視圖控制器,繼續將被執行。UIStoryboardSegue:一個UIStoryboardSegue對象負責執行過渡兩視圖控制器。
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//原視圖控制器
NSLog(@"Source Controller = %@", [segue sourceViewController]);
//目標視圖控制器
NSLog(@"Destination Controller = %@", [segue destinationViewController]);
//過渡標識
NSLog(@"Segue Identifier = %@", [segue identifier]);
if ([[segue identifier] isEqualToString:@"SimpleSegueToSecondViewController"]){
ZYSecondViewController *viewController = [segue destinationViewController];
viewController.str = @"sdfsdfdsfdsfs";
}
}
//通知視圖控制器,繼續將被執行。UIStoryboardSegue:一個UIStoryboardSegue對象負責執行過渡兩視圖控制器。
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//原視圖控制器
NSLog(@"Source Controller = %@", [segue sourceViewController]);
//目標視圖控制器
NSLog(@"Destination Controller = %@", [segue destinationViewController]);
//過渡標識
NSLog(@"Segue Identifier = %@", [segue identifier]);
if ([[segue identifier] isEqualToString:@"SimpleSegueToSecondViewController"]){
ZYSecondViewController *viewController = [segue destinationViewController];
viewController.str = @"sdfsdfdsfdsfs";
}
}
ZYSecondViewController.h
[plain]
#import <UIKit/UIKit.h>
@interface ZYSecondViewController : UIViewController
@property(nonatomic,copy)NSString *str;
@end
#import <UIKit/UIKit.h>
@interface ZYSecondViewController : UIViewController
@property(nonatomic,copy)NSString *str;
@end
ZYSecondViewController.m
[plain]
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//Test
NSLog(@"str=======>%@",str);
}
-(void)dealloc{
[str release];
[super dealloc];
}