我正在制作iOS应用,但我正在尝试将对象发送到另一个视图控制器,但它会抛出此错误
Blockquote- [UINavigationController setUserObject:]:无法识别的选择器发送到实例0x156aa330
这是我的代码: loginViewController.m 这是用户已经使用name,email和lastname
的值分配了对象#import "PerfilViewController.h"
#import "User.h"
@interface LogInViewController (){
User *user;
}
@end
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:@"loggedIn"]) {
user.name = self.nameText.text
user.lastname = self.lastNameText.text
PerfilViewController *pvc = [segue destinationViewController];
[pvc setUserObject: user];
}
}
在我的目标控制器中,我在viewDidLoad
上分配了它#import "PerfilViewController.h"
#import "User.h"
@interface PerfilViewController (){
User *user;
}
@end
@implementation PerfilViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setViewValues];
[self.navigationItem setHidesBackButton:YES];
user = [[User alloc] init];
}
#pragma mark - Segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
-(void)setUserObject:(User *)userToSet{
user = userToSet;
}
答案 0 :(得分:3)
问题是destinationViewController
是UINavigationViewController
所以PerfilViewController
应该是rootViewController
if([[segue identifier] isEqualToString:@"loggedIn"]) {
UINavigationController *navigationController = [segue destinationViewController];
PerfilViewController *pvc = navigationController.rootViewController;
[pvc setUserObject: user];
}
答案 1 :(得分:0)
通过查看提供的代码,我的猜测是[segue destinationViewController]
不属于班级PerfilViewController
。
您可以尝试检查pvc
是否有方法名称setUserObject
,然后在pvc
响应时调用该方法。如果没有,那么注销实际上是什么类pvc
来帮助您调试。这应该处理:
if ([pvc respondsToSelector:@selector(setUserObject)]) {
[pvc setUserObject: user];
} else {
NSLog(@"pvc is of class \"%@\"", [s class]);
}