Nuibb
Nuibb

Reputation: 1860

App crashes due to unrecognized selector sent to VC

My app crashing due to unrecognized selector sent with segue. I know it's a common question in stackoverflow. I tried all the solution but could not get through this. I think i'm missing something. My current project throws this exception, but a new project with this same code does not throw any exception. Why ? I tried like below code snippet to pass data from FirstViewController to DetailViewController using segue.

In my FirstViewController.h file :

 #import <UIKit/UIKit.h>
 #import "DetailViewController.h"

 @interface FirstViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

 @end

In my FirstViewController.m file :

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     //if ([segue.identifier isEqualToString:@"detail"]) {
         DetailViewController *vc = (DetailViewController *)[segue destinationViewController];
         vc.nameStr = @"Nuibb";
     //}
 }

In my DetailViewController.h file :

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController
@property (nonatomic, strong) NSString *nameStr;
@end

In my DetailViewController.m file :

#import "DetailViewController.h"

@interface DetailViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation DetailViewController
@synthesize nameStr;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.label.text = nameStr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

And i'm getting this error message in log -

2015-03-11 10:27:24.145 bdipo[1185:18722] -[UINavigationController setNameStr:]: unrecognized selector sent to instance 0x7fe928dad8c0
2015-03-11 10:27:24.147 bdipo[1185:18722] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setNameStr:]: unrecognized selector sent to instance 0x7fe928dad8c0'

Upvotes: 0

Views: 71

Answers (4)

Zahid
Zahid

Reputation: 562

On tableView didSelectRowAtIndex a UINavigationController is segued as per your storyboard and your navigationController is not having any property named nameStr

You will have to segue detailViewController direct. As UINavigationController cannot pe pushed in navigation.

Upvotes: 0

markedwardmurray
markedwardmurray

Reputation: 513

You only need one Navigation Controller in any app which loads at launch time and will handle navigating the full stack of view controllers for you. It needs to be the root view controller, with the "Is Initial View Controller" box checked (under Attributes Inspector, View Controller) to give it that start-arrow pointing in to the left side of the view.

enter image description here

Assuming your NewsTVC is the first view controller you want to show, delete the navigation view controller in your storyboard. Select NewsTVC, go up to your XCode toolbar and select "Editor --> Embed In --> Navigation Controller".

enter image description here

A navigation controller will be created that segues into your NewsTVC. Make sure "Is Initial View Controller" is selected for the new Navigation Controller, and drag a new segue between you NewsTVC prototype cell and the DetailVC. This should organize your stack so the segue runs properly.

enter image description here

Upvotes: 1

Saheb Roy
Saheb Roy

Reputation: 5957

V1(segue name = xyz)-->NavController-->(ROOT)VC2

In your vc1's didselectforRowAtIndexPath or any other Action method write this code.

[self performSegueWithIdentifire:@"xyz"];

Then override the method -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 if ([segue.identifier isEqualToString:@"xyz"]) {
     DetailViewController *vc = (DetailViewController *)[segue destinationViewController];
     vc.nameStr = @"Nuibb";
 }
}

Inside DetailViewController.h declare a property first.

@property(nonatomic,strong)NSString *nameStr;

Upvotes: 1

Vidhyanand
Vidhyanand

Reputation: 5369

You can access your view as below

DetailViewController *detailVC =
        [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];//modify this identifier name as per your StoryBoardIDentifier for detailview at storyboard..
detailVC.nameStr=@"Nuibb";
[self.navigationController pushViewController:detailVC animated:NO];//if you want to push to detail view from first view

Hope it helps you...

Upvotes: 1

Related Questions