上一次我们介绍了Storyboard的简单创建方法,这次我们来介绍一下如何通过Storyboard进行两个ViewController之间的传值。
ZYViewController.m
//通知视图控制器,继续将被执行。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
#import <UIKit/UIKit.h> @interface ZYSecondViewController : UIViewController @property(nonatomic,copy)NSString *str; @end
ZYSecondViewController.m
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //Test NSLog(@"str=======>%@",str); } -(void)dealloc{ [str release]; [super dealloc]; }
运行结果:
点击Button后控制台结果:
2013-05-09 14:10:15.850 StoryboardsTest[610:c07] Source Controller = <ZYViewController: 0x75668d0>
2013-05-09 14:10:15.850 StoryboardsTest[610:c07] Destination Controller = <ZYSecondViewController: 0x8e25730>
2013-05-09 14:10:15.851 StoryboardsTest[610:c07] Segue Identifier = SimpleSegueToSecondViewController
2013-05-09 14:10:15.852 StoryboardsTest[610:c07] str=======>sdfsdfdsfdsfs
3 结语
以上就是主要内容,希望对大家有所帮助。
Demo代码下载:http://download.csdn.net/detail/u010013695/5348653