ps:在presentModalViewController或者pushviewController之前,都是不会加载该view的nib文件的,
也就没有new IBOutlet的实体对象,所以在此之前使用IBOutlet对象保存数据都是错误的行为。需要
一个中间的成员变量来做中介,才能正确的传递数据在view之间。
Passing Data Between Views
In this tutorial i will be showing you how to create multi functioning buttons
Features:
- 1 Label
- 1 UITextField
- 2 Buttons
- 2 Views
Passing dada between views is very handy if you require content to move throughout you application its also very helpful for games where your score or info can be passed to the next view where theres leader boards or extra content
The Code
Play the video to get a step by step walkthrough and all code can be copy and pasted |
ViewController.h
- #import <UIKit/UIKit.h>
- #import "secondview.h"
- @interface ViewController : UIViewController
- {
- secondview *secondviewData;
- __weak IBOutlet UITextField *textfield;
- }
- @property (nonatomic,retain)secondview *secondviewData;
- - (IBAction)passdata:(id)sender;
- @end
ViewController.m
- #import "ViewController.h"
- #import "secondview.h"
- @implementation ViewController
- @synthesize secondviewData;
- -(IBAction)passdata:(id)sender {
- secondview *second = [[secondview alloc] initWithNibName:nil bundle:nil];
- self.secondviewData = second;
- secondviewData.passedValue = textfield.text;
- [self presentModalViewController:second animated:YES];
- }
Secondview.h
- #import <UIKit/UIKit.h>
- @interface secondview : UIViewController
- {
- __weak IBOutlet UILabel *label;
- NSString *passedValue;
- }
- @property (nonatomic,retain)NSString *passedValue;
- - (IBAction)back:(id)sender;
- @end
Secondview.m
- #import "ViewController.h"
- @implementation secondview
- @synthesize passedValue;
- -(IBAction)back:(id)sender {
- ViewController *second = [[ViewController alloc] initWithNibName:nil bundle:nil];
- [self presentModalViewController:second animated:YES];
- }
- - (void)viewDidLoad
- {
- label.text = passedValue;
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- }
视频:http://www.youtube.com/watch?v=Y4zmM_Rqv6Y&feature=player_embedded