Horray
Horray

Reputation: 693

prepare for segue is not letting me set NSString of next viewController

I want to set an NSString from viewController A to viewController B. I tried using the code below but it's not working.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"segueSubCat"])
    {
        NSIndexPath *indexPath = [tableViewCat indexPathForSelectedRow];

        ViewControllerSubCat *subCatVC = [segue destinationViewController];
        subCatVC.cellNameCat = [categories objectAtIndex:indexPath.row];
        subCatVC.navigationItem.title = subCatVC.cellNameCat;
    }
}

I tried that, and the app crashed every time I went to the second view. So I replaced it with the following code:

wat = @"wat";
[[segue destinationViewController] setContentsName:wat];

And it did the same thing. i.e. it crashed. I'm not sure what I'm doing wrong.

Note: When I take away the code inside prepareForSegue, then it works.

Error Message

2014-12-18 15:36:16.382 myApp[1503:42438] -[UINavigationController setCellNameCat:]: unrecognized selector sent to instance 0x7fb9714e7240
2014-12-18 15:36:16.388 myApp[1503:42438] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setCellNameCat:]: unrecognized selector sent to instance 0x7fb9714e7240'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010094cf35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001005e5bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010095404d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001008ac27c ___forwarding___ + 988
    4   CoreFoundation                      0x00000001008abe18 _CF_forwarding_prep_0 + 120
    5   myApp        0x00000001000b3887 -[ViewController prepareForSegue:sender:] + 343
    6   UIKit                               0x000000010128b71c -[UIStoryboardSegueTemplate _perform:] + 151
    7   UIKit                               0x0000000100e21360 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1242
    8   UIKit                               0x0000000100e214d4 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
    9   UIKit                               0x0000000100d5c331 _applyBlockToCFArrayCopiedToStack + 314
    10  UIKit                               0x0000000100d5c1ab _afterCACommitHandler + 516
    11  CoreFoundation                      0x0000000100881dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    12  CoreFoundation                      0x0000000100881d20 __CFRunLoopDoObservers + 368
    13  CoreFoundation                      0x0000000100877b53 __CFRunLoopRun + 1123
    14  CoreFoundation                      0x0000000100877486 CFRunLoopRunSpecific + 470
    15  GraphicsServices                    0x0000000103f1b9f0 GSEventRunModal + 161
    16  UIKit                               0x0000000100d39420 UIApplicationMain + 1282
    17  myApp        0x00000001000b4523 main + 115
    18  libdyld.dylib                       0x0000000102edc145 start + 1
    19  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Upvotes: 1

Views: 176

Answers (2)

Duncan C
Duncan C

Reputation: 131471

As the other posters have said, the destination view controller is not your custom class. It sounds like you've got that sorted out.

Another problem you're likely to face:

At the time prepareForSegue is called, the destination VC's view hierarchy hasn't been loaded yet.

This line:

subCatVC.navigationItem.title = subCatVC.cellNameCat;

probably won't work, because I doubt if your new VCs navigationItem has been loaded yet.

Disclaimer: I've actually never tried to manipulate a destination view controller's navigationItem in a prepareForSegue method, so I don't know for sure if it will be defined, but I strongly suspect not. To test this, log the value of subCatVC.navigationIte in prepareForSegue (once you sort out getting a pointer to the correct VC)

Upvotes: 0

Midhun MP
Midhun MP

Reputation: 107201

First check whether you assigned the UIViewController class as ViewControllerSubCat in storyboard.

EDIT:

From your crash log it's clear that you are getting a UINavigationController as the result of [segue destinationViewController];. So change the code like:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"segueSubCat"])
    {
        NSIndexPath *indexPath = [tableViewCat indexPathForSelectedRow];
        UINavigationController *navController = [segue destinationViewController];
        ViewControllerSubCat *subCatVC = (ViewControllerSubCat *)navController.topViewController;
        subCatVC.cellNameCat = [categories objectAtIndex:indexPath.row];
        subCatVC.navigationItem.title = subCatVC.cellNameCat;
    }
}

Upvotes: 1

Related Questions