我试图在一个视图中输入在UITextFields中输入的文本,并在按下按钮时将这些值传递给另一个View Controller。在第一个视图控制器(CreateViewController)中,我有:
#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
实施文件是:
#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
但是,在连接所有插座后,ThePreviewViewController中的标签保持为null。想法?
答案 0 :(得分:2)
看起来这是由于之前讨论过的一个问题:当您实例化ThePreviewViewController
时,其UIKit实例尚不可用于设置其属性。
解决方案是在NSString
(或更紧凑地使用NSDictionary)上创建和设置ThePreviewViewController
属性,然后在其ViewWillAppear
事件处理程序中使用它来设置其属性UIKit元素。
答案 1 :(得分:1)
您应该设置NSString属性,而不是在 ThePreview VC 中设置UITextField文本。这是因为UITextField可能尚未加载, _thePreview.issue.text = issue.text 将无效,因为 _thePreview.issue 尚未创建。因此,在 ThePreview VC 中创建一个属性(例如NSString * issueString),然后在 CreateViewController中将该属性设置为 _thePreview.issueString = issue.text ,最后在 ThePreview VC 中使用 self.issue.text = self.issueString 在 viewDidLoad 方法中设置textField文本;
希望这有帮助。