将数据从 ViewController 传递到实例化自定义 viewcontroller

Passing Data from ViewController to instantiated custom viewcontroller

我正在尝试将数据从我的数据库传递到在此函数中实例化的新 viewcontroller 中的 UILabels。我已经测试过数据是否被正确抓取,并且工作正常。

我相信我没有在我的 DataViewController 中正确引用 UILabel 来进行实例化。当我尝试 NSLog (dataViewController.cartAddress)

时得到空值

有什么帮助吗?

ViewController.m

- (void) mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:    (GMSMarker *)marker {
NSObject_DatabaseHelper *dataToPush = [NSObject_DatabaseHelper getSharedInstance];
NSArray *cartDataToPush = [dataToPush findByName:marker.title];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
DataViewController *dataViewController = (DataViewController *)[storyboard instantiateViewControllerWithIdentifier:@"dataViewController"];

dataViewController.cartAddress.text = cartDataToPush[0];
dataViewController.thumbsUp.text = cartDataToPush[3];
dataViewController.thumbsDown.text = cartDataToPush[4];
dataViewController.pitaBool.text = cartDataToPush[5];
dataViewController.drinkBool.text = cartDataToPush[6];
dataViewController.greenSauceBool.text = cartDataToPush[7];


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

当您实例化 viewController 时,它还没有设置它的 UI。 因此,在 viewController 的 viewDidLoad 回调触发之前,您将无法与 UILabel 进行交互。所以,做你想做的最好的方法是设置 NSString 属性,并在 viewDidLoad 中将它们设置为 UILabels。类似的东西

@property (nonatomic, strong) NSString *cartAddressText;
/* ... */

然后在viewDidLoad

- (void) viewDidLoad {
     [super:viewDidLoad];
     self.cartAddress.text = self.cartAddressText;
     /* ... */
}