0

I am trying to switch from a table view to a detail view. When I click on the cell it crashes. I have used breaks to try and find where in the code it crashes but I cant find a specific line. It crashes when the segue is performed from the table view to the detail view. I would like to pass some variables to the detail view but I first need to get it to switch to the detail view. I am using storyboard fyi. Any help would be appreciated.

UPDATE: The crash log is as follows

arg c = (int) 1
arg v = (char **) 0x2fd63cdc

This is the line the crash is on

return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

UPDATE 2

The last section of code before it crashes when I am stepping through the code is the prepare for segue function. After the last line of code in there it crashes and goes to the line above.

UPDATE 3

I realized the console was hidden so this is the error that shows up in the console. Both of my nslogs in prepare for segue were called.

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<DetailViewController 0x1e098bd0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name.'
*** First throw call stack:
(0x3a1e63e7 0x39072963 0x3a1e60d5 0x3a418fe9 0x3a414d53 0x3a16c8a5 0x39665d7d 0x396655ff 0x3955e039 0x394e8421 0x3953d1b7 0x3953d0fd 0x3953cfe1 0x3953cf0d 0x3953c659 0x3953c541 0x3952ab33 0x3952a7d3 0x3958b2ad 0x3960dca1 0x3a4b9e67 0x3a1bb857 0x3a1bb503 0x3a1ba177 0x3a12d23d 0x3a12d0c9 0x3746633b 0x3951d291 0x69a9 0x3a91eb20)
libc++abi.dylib: terminate called throwing an exception

RootViewController.m

#import "RootViewController.h"
#import "DetailViewController.h"



@implementation RootViewController

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

- (void)viewDidLoad {
    [super viewDidLoad];

    //Initialize the array.
    listOfItems = [[NSMutableArray alloc] init];

    //Add items
    [listOfItems addObject:@"Jane"];
    [listOfItems addObject:@"Johnny"];
    [listOfItems addObject:@"Deanne"];
    [listOfItems addObject:@"John"];
    [listOfItems addObject:@"Susan"];

    //Set the title
    self.navigationItem.title = @"Countries";
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [listOfItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"MyCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Set up the cell...
    NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;


    return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    DetailViewController *DVC = [[DetailViewController alloc]init];
    DVC = [segue destinationViewController];
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    NSString *name = [listOfItems objectAtIndex:path.row];
    DVC.name1.text = name;
}

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController <UITableViewDataSource,UITableViewDelegate> {

    NSMutableArray *listOfItems;
    IBOutlet NSMutableArray *detailListOfItems;
}
@end

DetailViewController.m

#import "DetailViewController.h"



@implementation DetailViewController
@synthesize selectedCountry;
@synthesize name1;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];


    //Set the title of the navigation bar
    //self.navigationItem.title = name1.text;

}

DetailViewController.h

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController


@property (nonatomic,strong) IBOutlet UILabel *name1;
@property (nonatomic, retain) NSString *selectedCountry;
@end
Johnny Cox
  • 1,873
  • 3
  • 17
  • 17
  • Not enough crash log. Need way more detail than that. Or at least the reason why it crashed in terms of the code - was it a SEGV (segmentation fault) or exception? – Kendall Helmstetter Gelner Dec 25 '12 at 03:41
  • In your `prepareForSegue`, you're trying to set the text property of the control called `name1`. You can't access the controls in the `destinationViewController`, because it's view hasn't been created yet. You have to have add a `NSString` property to your destination controller, you set that in `prepareForSegue`, and then `viewDidLoad` of your destination controller can set the `text` property of your `name1` control to value that was stored in the `NSString` property that you set in `prepareForSegue`. – Rob Dec 25 '12 at 05:34

2 Answers2

5

Add an exception breakpoint....

Here is the tab at the top:

enter image description here

You need to select the 2nd last breakpoint icon one. Press that and look at the bottom for the add icon:

enter image description here

Then press add and create an exception breakpoint and modify it to these settings:

enter image description here

And heres your breakpoint exception!

enter image description here

Then run your app and it should crash on your line where it crashes. You may see some crash log, and its possible you have to change the scheme to GDB


Update

Is there any sort of crash log or stack trace? Also, try changing the scheme to GDB... heres how:

enter image description here

enter image description here

enter image description here

Switching to GDB usually fixes this. If you are already using GDB, switch to LLDB and see what happens!

Note: as you posted,

arg c = (int) 1

arg v = (char **) 0x2fd63cdc

Is not your crash log...


Update

If this still does not work you should put breakpoints and NSLog()'s all over the place to debug and find out your bad crashy code's position!


Update

Look we obviously can't find out where it is crashing, but I might have a subtle clue on where the thing is crashing and why. Its related to your table view and listOfItems. Usually a table view reloads its data and calls its delegate and data source methods just before or simultaneously viewDidLoad code gets called. So transfer the initialization and adding of objects of listOfItems to viewWillAppear and see what happens.


Update

Check all your outlets in your storyboard and in DetailViewController and make sure they are not connected to anything that doesn't exist anymore

MCKapur
  • 9,127
  • 9
  • 58
  • 101
  • This is the line of code it always crashes on. Same as when I added the exception break point – Johnny Cox Dec 25 '12 at 03:17
  • return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); – Johnny Cox Dec 25 '12 at 03:18
  • @JohnnyCox Is there a crash log? Updated question – MCKapur Dec 25 '12 at 03:20
  • I switched it to GDB and it still does the same thing. And as far as my crash log that is all that pops up when it crashes. Is there somewhere else to look? – Johnny Cox Dec 25 '12 at 03:31
  • @JohnnyCox Can you screenpic that and update it with your question... just to confirm? Also, I have another update of an answer – MCKapur Dec 25 '12 at 03:34
  • I switched initialization to viewDidAppear method and doing this doesnt load the names into the table view. – Johnny Cox Dec 25 '12 at 03:46
  • @JohnnyCox Try 'viewWillAppear' and inside the method you have to write '[super viewWillAppear: YES];' – MCKapur Dec 25 '12 at 03:48
  • Ok did that. The items get loaded into the table but same crash. It says thread 1: program received signal "SIGABRT". – Johnny Cox Dec 25 '12 at 03:50
  • @JohnnyCox This is extremely strange... I have just reread your question, it happens when you select your cell. CAn you put an NSLog in the beginning and end of the prepareForSegue: method and see what gets called. Also make sure all your outlets are properly connected in .xib if you have any – MCKapur Dec 25 '12 at 03:55
  • @RohanKapur Updated questions above. And I am using storyboard if that helps at all. – Johnny Cox Dec 25 '12 at 04:01
  • Sorry to be such a pain. I've been trying to figure this out for days. – Johnny Cox Dec 25 '12 at 04:13
  • Wow thank you so much. I had a label in the detailViewController that was connected to an old variable. Thanks a lot. – Johnny Cox Dec 25 '12 at 04:16
  • @JohnnyCox keep up the good question asking, there are a lot of pathetic question askers on this site but you really understand! – MCKapur Dec 25 '12 at 04:29
0

Heres the solution: Check the name of your cell identifier and apply this identifier to your segue.Every Segue has an identifier..default is empty..so try assigning a name thats similar to your code,particularly the segue thats going to your detail view controller.