Cesar Mtz
Cesar Mtz

Reputation: 322

IOS unrecognized selector on Object Segue

I'm making an iOS app but I'm trying to send an object to another view controller but it throws me this error

Blockquote-[UINavigationController setUserObject:]: unrecognized selector sent to instance 0x156aa330

Here's my code: loginViewController.m this is user it's already allocated the object with the values of name, email, and 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];
    }
}

In my destination controller I have this its as well allocated on 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;
}

Upvotes: 2

Views: 131

Answers (2)

Mahmoud Adam
Mahmoud Adam

Reputation: 5852

The problem is that the destinationViewController is a UINavigationViewController so PerfilViewController should be its rootViewController

if([[segue identifier] isEqualToString:@"loggedIn"]) {
    UINavigationController *navigationController = [segue destinationViewController];
    PerfilViewController *pvc = navigationController.rootViewController;
    [pvc setUserObject: user];
}

Upvotes: 3

Fennelouski
Fennelouski

Reputation: 2431

My guess by looking at the code provided is that [segue destinationViewController] is not of the class PerfilViewController.

you could try checking to see if pvc has a method names setUserObject and then call that method if pvc responds to it. If not, then log out what class pvc actually is to help you debug. This should handle that:

if ([pvc respondsToSelector:@selector(setUserObject)]) {
    [pvc setUserObject: user];
} else {
    NSLog(@"pvc is of class \"%@\"", [s class]);
}

Upvotes: 0

Related Questions