为什么此视图控制器无法设置下一个视图控制器的文本字段? [英] Why can't this view controller set the text fields of the next view controller?
问题描述
我有一个视图控制器,该控制器具有带完成按钮的导航栏和两个文本字段.按下完成按钮时,将调用方法 postInfo
.这是实现:
I have a view controller that has navigation bar with a done button, and two text fields. When the done button is pressed, the method postInfo
is called. Here is the implementation:
- (void)postInfo{
ListingViewController* lvc = [[ListingViewController alloc] init];
NSString* listingName = listingNameField.text;
NSString* listingPrice = listingPriceField.text;
NSLog(@"%@", listingName);
NSLog(@"%@", listingPrice);
[lvc.titleLabel setText:listingName];
[lvc.priceLabel setText:listingPrice];
[self.navigationController pushViewController:lvc animated:YES];
}
此处 ListingViewController.h
:
#import <UIKit/UIKit.h>
@interface ListingViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@end
UILabel是通过xib文件设置的,并且为空.将根据要求发布所需的任何其他代码.
The UILabel's are set through a xib file and are empty. Will post whatever other code is needed upon request.
推荐答案
在您执行该推送时,尚未加载下一个控制器的视图,因此您无法访问其视图.您需要在ListingViewController中创建NSString属性,并将字符串传递给postnfo方法中的那些属性.然后在ListingViewController的viewDidLoad方法中,使用这些属性填充标签(这些标签将在viewDidLoad运行时加载).
At the time you do that push, the next controller's view hasn't been loaded yet, so you can't access its views. You need to create NSString properties in ListingViewController and pass a string to those in your postnfo method. Then in ListingViewController's viewDidLoad method, use those properties to populate the labels (which will have been loaded by the time viewDidLoad runs).
这篇关于为什么此视图控制器无法设置下一个视图控制器的文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!