[英]How to pass data to collection view controller embedded in navigation controller
[英]How to Pass Data Object to View Controller Embedded in Navigation Controller in Different Storyboard
当视图控制器位于不同的故事板中时,我的代码通常可以很好地将对象传递给另一个视图控制器。
我的代码加载了嵌入在其导航控制器中的正确视图控制器,但没有任何数据。 (数据对象在目标 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。 我只是无法传递自定义对象(例如用户),或者我创建了另一个 NSObject 以在嵌入导航时向 VC 进行测试。 也许这是使用不同故事板时的一些奇怪故障。
编辑 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
在调试器显示零po self.user
在调试器显示零
如果没有看到您的代码的完整示例,我不确定,但我您一定错过了一些东西......
这是一个完整的例子。 与@IBOutlet
和@IBAction
(以及 Storyboard ID)相关联的内容应该很明显:
用户对象.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
用户对象.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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.