I am trying to take the text that is typed into UITextFields in one view, and pass those values into a different View Controller when a button is pressed. In the first view controller (CreateViewController) I have:
#import <UIKit/UIKit.h>
#import <MessageUI/MFMailComposeViewController.h>
@class ThePreviewViewController;
@interface CreateViewController : UIViewController <UIAlertViewDelegate, MFMailComposeViewControllerDelegate> {
UIDatePicker *datePicker;
UIDatePicker *datePicker2;
ThePreviewViewController *_thePreview;
}
@property (nonatomic, retain) IBOutlet UITextField *toName;
@property (nonatomic, retain) IBOutlet UITextField *fromName;
@property (nonatomic, retain) IBOutlet UITextField *issue;
@property (nonatomic, retain) ThePreviewViewController *thePreview;
@property (nonatomic, retain) IBOutlet UITextField *expire;
@property (nonatomic, retain) IBOutlet UITextField *loveMessage;
@property (nonatomic, retain) IBOutlet UITextField *gift;
-(IBAction)build;
@end
The implementation file is:
#import "ThePreviewViewController.h"
@implementation CreateViewController
@synthesize toName, fromName, gift, loveMessage, issue, expire, datePicker, datePicker2;
@synthesize thePreview = _thePreview;
-(IBAction)preview {
if (_thePreview == nil) {
self.thePreview = [[ThePreviewViewController alloc] initWithNibName:@"ThePreviewViewController" bundle:[NSBundle mainBundle]] ;
}
_thePreview.issue.text = issue.text;
_thePreview.gift.text = gift.text;
_thePreview.expire.text = expire.text;
[self.navigationController pushViewController:_thePreview animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
However, after I connect all the outlets, the labels in ThePreviewViewController stay at null. Thoughts?
It looks like this is due to an issue which has been discussed here before: when you instantiate your
ThePreviewViewControllerits UIKit instances are not yet available to have their properties set.The solution is to create and set
NSStringproperties on yourThePreviewViewController(or more compactly use an NSDictionary) which then use, in itsViewWillAppearevent handler to set the properties of its UIKit elements.