17

I'm using storyboard for an iOS application, my storyboard looks like this: http://d.pr/7yAY (droplr url)

The problem is when I click the login button, I send the username captured to the events table view controller. To do that I use prepareForSegue function, but apparently when I try to set the username in throws an exception.

My code is as follows:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (IBAction) logintButton:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *username_tf; // textfield

@end

ViewController.m

#import "ViewController.h"
#import "EventsTableViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize username_tf;


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"ToMainApp"])
    {
        EventsTableViewController * dest = (EventsTableViewController *)[segue destinationViewController];
        NSString * username = [[NSString alloc] initWithFormat:@"%@", username_tf.text];
        [dest setUsername:username];
    }
}


- (IBAction) logintButton:(id)sender
{
    //NSLog(@"Logint button pressed");
    [self performSegueWithIdentifier:@"ToMainApp" sender:sender];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setUsername_tf:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

EventsTableViewController.h

#import <UIKit/UIKit.h>

@interface EventsTableViewController : UITableViewController
{
    NSString * username;
}

@property (nonatomic, retain) NSString * username;

@end

EventsTableViewController.m

#import "EventsTableViewController.h"

@interface EventsTableViewController ()
@end

@implementation EventsTableViewController

@synthesize username;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

...

@end

The throwed exception is:

2012-03-15 14:19:27.304 StoryboardAssistance[30989:f803]
-[UINavigationController setUsername:]: unrecognized selector sent to instance 0x68abf60 2012-03-15 14:19:27.306
StoryboardAssistance[30989:f803] *** Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason:
'-[UINavigationController setUsername:]: unrecognized selector sent to
instance 0x68abf60'
*** First throw call stack: (0x13c9022 0x155acd6 0x13cacbd 0x132fed0 0x132fcb2 0x28e6 0x43e4be 0xdb5ab 0x2974 0x13cae99 0x1614e 0x160e6
0xbcade 0xbcfa7 0xbc266 0x3b3c0 0x3b5e6 0x21dc4 0x15634 0x12b3ef5
0x139d195 0x1301ff2 0x13008da 0x12ffd84 0x12ffc9b 0x12b27d8 0x12b288a
0x13626 0x253d 0x24a5) terminate called throwing an exception(lldb)

Any suggestions?

2
  • Can you do a step debug and figure out which line it is actually crashing on? Also NSString * username = [[NSString alloc] initWithFormat:@"%@", username_tf.text]; can just be NSString * username = username_tf.text.
    – Gobot
    Mar 15, 2012 at 20:43
  • Can you update the image of your storyboard? It's currently 404ing. Thanks
    – cwiggo
    Apr 27, 2015 at 9:33

3 Answers 3

33

Your segue's destination view controller is your Navigation Controller, not your Events Table View controller.

You can get the Events controller by accessing the Navigation controller's topViewController property.

Try this:

UINavigationController *navController = (UINavigationController*)[segue destinationViewController];
EventsTableViewController *eventsController = [navController topViewController];

NSString * username = [[NSString alloc] initWithFormat:@"%@", username_tf.text];
[eventsController setUsername:username];
0
16

Alternatively, try this:-

(It does the same as jonkroll's answer but in one line and removes the warning "Incompatible pointer types initializing 'ViewController *__strong' with an expression of type UIViewController *"

 NameOfViewController *vc = (NameOfViewController *)[[segue destinationViewController] topViewController];
1
  • 1
    Great one line solution. Worked for me!
    – Groot
    Dec 12, 2012 at 9:42
4

Another thing to verify is that you have properly assigned your destination ViewController as the proper class in Interface Builder. By default this will be UIViewController or UITableViewController etc. If you have a custom class with getters/setters, you need to remember to change the class in the Interface Builder to reflect accordingly, otherwise you will receive an invalid selector error message.

1
  • I feel stupid that I forgot to do this, thanks for reminding.
    – shehanrg
    Dec 25, 2014 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.