IOS在Object Segue上无法识别选择器

时间:2015-05-26 17:19:30

标签: ios objective-c iphone

我正在制作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;
}

2 个答案:

答案 0 :(得分:3)

问题是destinationViewControllerUINavigationViewController所以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]);
}