如何将数据对象传递给嵌入在不同情节提要中的导航控制器中的视图控制器

How to Pass Data Object to View Controller Embedded in Navigation Controller in Different Storyboard

当视图控制器位于不同的情节提要中时,我的代码通常可以很好地将对象传递给另一个视图控制器,但无法正常工作。

我的代码加载了嵌入其导航控制器中的正确视图控制器,但没有任何数据。 (目标中的数据对象为 nil VC)。

这是我尝试使用的代码;

UIStoryboard *sb2 = [UIStoryboard storyboardWithName:@"secondSB" bundle:nil];
UINavigationController* nav = [sb2 instantiateViewControllerWithIdentifier:@"userNav"];
userDetail *destVC = (userDetail * )nav.topViewController;
NSLog(@"user name%@",user.name);//Logs name showing the user is not empty
destVC.user = user;
[self presentViewController:nav animated:YES completion:nil];

上面加载了没有数据的 VC。

如果我在没有导航控制器的情况下直接显示 VC,我可以将数据对象传递给 VC。但那样的话,我就失去了我需要的导航功能。

UIStoryboard *sb2 = [UIStoryboard storyboardWithName:@"secondSB" bundle:nil];
userDetail *destVC = [sb2 instantiateViewControllerWithIdentifier:@"userDetail"];
NSLog(@"user name%@",user.name);//Logs name showing the user is not empty
destVC.user = user;
[self presentViewController:destVC animated:YES completion:nil];

以上代码可能有什么问题,我应该使用什么代码。

编辑:

我能够将常规对象(例如字符串)传递给导航中嵌入的 VC。或者我可以将未嵌入导航的自定义对象传递给 VC。当嵌入导航时,我无法将自定义对象(例如用户)传递给 VC,或者我创建了另一个 NSObject 进行测试。也许这是使用不同的故事板时出现的一些奇怪的故障。

编辑 2

这是我创建的轻型用户的对象代码,以防原始用户对象出现问题:

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface lightUser : NSObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * userid;
@property (nonatomic, retain) NSString * descript;

@end

这是VC中的一个属性:

#import "lightServer.h"
//in interface
@property (weak, nonatomic) lightUser* user;

ViewDidLoad 中的以下代码没有任何效果,用户显示为 nil:

self.user.name = @"Hello there";//

po self.user.name 在调试器中显示为 nil po self.user 在调试器中显示 nil

在没有看到您的代码的完整示例的情况下不确定,但我一定是您遗漏了一些东西...

这是一个完整的例子。连接到 @IBOutlet@IBAction(以及故事板 ID)的内容应该很明显:

UserObject.h

//
//  UserObject.h
//  Created by Don Mag on 4/1/20.
//

#import <Foundation/Foundation.h>

@interface UserObject : NSObject

@property (strong, nonatomic) NSString *firstName;
@property (strong, nonatomic) NSString *lastName;
@property (assign, readwrite) NSInteger age;

- (NSString *)name;

@end

UserObject.m

//
//  UserObject.m
//  Created by Don Mag on 4/1/20.
//

#import "UserObject.h"

@implementation UserObject

- (NSString *)name {
    return [NSString stringWithFormat:@"%@, %@", _lastName, _firstName];
}

@end

** FirstViewController.h**

//
//  FirstViewController.h
//  Created by Don Mag on 4/1/20.
//

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController
@end

** FirstViewController.m**

//
//  FirstViewController.m
//  Created by Don Mag on 4/1/20.
//

#import "FirstViewController.h"
#import "UserDetailViewController.h"
#import "UserObject.h"

@interface FirstViewController ()

@property (strong, nonatomic) UserObject *aUserObject;
@property (assign, readwrite) NSInteger iAge;

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // initialize age
    _iAge = 25;

    // initialize a new UserObject
    _aUserObject = [UserObject new];

    _aUserObject.firstName = @"John";
    _aUserObject.lastName = @"Smith";
    _aUserObject.age = _iAge;

}

- (IBAction)didTap:(id)sender {

    UIStoryboard *sb2 = [UIStoryboard storyboardWithName:@"secondSB" bundle:nil];
    UINavigationController* nav = [sb2 instantiateViewControllerWithIdentifier:@"userNav"];

    UserDetailViewController *destVC = (UserDetailViewController * )nav.topViewController;

    // increment age, so it changes each time we call this method
    _iAge++;

    _aUserObject.age = _iAge;

    destVC.userObj = _aUserObject;

    [self presentViewController:nav animated:YES completion:nil];

}

@end

UserDetailViewController.h(VC 在第二个故事板中)

//
//  UserDetailViewController.h
//  Created by Don Mag on 3/31/20.
//

#import <UIKit/UIKit.h>

#import "UserObject.h"

@interface UserDetailViewController : UIViewController

@property (strong, nonatomic) UserObject *userObj;

@end

UserDetailViewController.m

//
//  UserDetailViewController.m
//  Created by Don Mag on 3/31/20.
//

#import "UserDetailViewController.h"

@interface UserDetailViewController ()
@property (strong, nonatomic) IBOutlet UILabel *userLabel;
@end

@implementation UserDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _userLabel.text = [NSString stringWithFormat:
                       @"_useObj.firstName: %@ \n" \
                       "_userObj.lastName: %@ \n" \
                       "_userObj.age: %ld \n" \
                       "_userObj name method: %@",
                       _userObj.firstName,
                       _userObj.lastName,
                       _userObj.age,
                       [_userObj name]];

}

@end

如果不是很清楚,这里是一个工作示例应用程序:https://github.com/DonMag/DataObjectPassing