简体   繁体   English

为什么这个视图控制器不能设置下一个视图控制器的文本字段?

[英]Why can't this view controller set the text fields of the next view controller?

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. 按下完成按钮后,将调用postInfo方法。 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]; 

} }

Here ListingViewController.h : 这里的ListingViewController.h

#import <UIKit/UIKit.h>

@interface ListingViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@end

The UILabel's are set through a xib file and are empty. UILabel是通过xib文件设置的,是空的。 Will post whatever other code is needed upon request. 将根据要求发布任何其他代码。

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. 您需要在ListingViewController中创建NSString属性,并将字符串传递给postnfo方法中的字符串。 Then in ListingViewController's viewDidLoad method, use those properties to populate the labels (which will have been loaded by the time viewDidLoad runs). 然后在ListingViewController的viewDidLoad方法中,使用这些属性来填充标签(这些标签将在viewDidLoad运行时加载)。

Change your code to this. 将您的代码更改为此。

- (void)postInfo
{
  ListingViewController* lvc = [[ListingViewController alloc] init];
  [lvc view];    // loads the view 

  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]; 
}

This happens because until the view property of the view controller is accessed, the view will not be loaded and all of the subviews are nil. 发生这种情况是因为在访问视图控制器的view属性之前,将不会加载视图并且所有子视图都是nil。 They can be configured after viewDidLoad is called on the view controller being pushed. 可以在推送视图控制器上调用viewDidLoad后配置它们。 Calling [lvc view] loads the view immediately. 调用[lvc view]会立即加载视图。

I usually get this too (in the example of when using segues). 我通常也会得到这个(在使用segues的例子中)。 I get around it by setting an NSString property instead of an IBOutlet during prepareForSegue of the destinationController . 我通过在destinationController prepareForSegue期间设置NSString属性而不是IBOutlet来解决它。 Then during viewDidLoad of the next View Controller, I take the value of the property and assign it to the UILabel . 然后在下一个View Controller的viewDidLoad中,我获取属性的值并将其分配给UILabel

You may find the explanation in this answer useful: https://stackoverflow.com/a/8094146/2358334 您可能会发现此答案中的解释很有用: https//stackoverflow.com/a/8094146/2358334

If you set a breakpoint just after the line you call 如果在您调用的行之后设置断点

testViewController *viewController = segue.destinationViewController;

when you build and run the project, you will find that the UITextField property in the destinationViewController is not allocated and initiated (memory is 0x0) at the breakpoint. 在构建和运行项目时,您会发现destinationViewController中的UITextField属性未在断点处分配和启动(内存为0x0)。 Meanwhile the NSString property is already allocated and initialised (so you can set its value). 同时NSString属性已经分配并初始化(因此您可以设置其值)。

Try to do this 试着这样做

first you need to load the view (push viewcontroller) and then you can access the properties (because you have created views by IBOutlet 首先你需要加载视图(推送viewcontroller),然后你可以访问属性(因为你已经通过IBOutlet创建了视图

- (void)postInfo{
    ListingViewController* lvc = [[ListingViewController alloc] init];

    NSString* listingName = listingNameField.text;
    NSString* listingPrice = listingPriceField.text;

    NSLog(@"%@", listingName);
    NSLog(@"%@", listingPrice); 

    [self.navigationController pushViewController:lvc animated:YES];

    [lvc.titleLabel setText:listingName];
    [lvc.priceLabel setText:listingPrice]; 

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM