Search code examples
objective-cios7restkitunrecognized-selector

Programmatically navigate view controllers from the context of Restkit success


I'm trying to dry up my code, so I have pulled restkit methods out of the ViewController into their own model. Therefore on success, I want to:

  1. Pass the current view controller self, to the function
  2. Instantiate a new view controller
  3. Navigate between view controllers - from the context of the success function in an API object

I call this code from my view controller:

@interface snapViewController ()
@property (strong, nonatomic) snapApi *api;
@property (weak, nonatomic) IBOutlet UITextField *userEmail;
@property (weak, nonatomic) IBOutlet UITextField *userPassword;  
@end

@implementation snapViewController
- (IBAction)firePostCreateAtApi:(id)sender {
    [self.api createNewUser:self.userEmail.text userPassword:self.userPassword.text viewController:self];

}

Here is the code (failing code) Check out success:

// Hit the CreateNewUser Endpoint

- (void)createNewUser:(NSString *)userName userPassword:(NSString *)password viewController:viewController
{
    NSString *token = SNAPTOKEN;
    NSDictionary *profileQueryParams = @{
                              @"password":password,
                              @"email":userName,
                              @"headline":@"headline"};

    NSDictionary *queryParams = @{@"accesstoken" : token,
                                  @"profile" : profileQueryParams};

    [[RKObjectManager sharedManager] postObject: nil
                                           path: @"/new"
                                     parameters:queryParams
                                        success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                            NSLog(@"viewcontroller: %@", viewController);
                                            snapHomeViewController *snap = [[snapHomeViewController alloc] initWithNibName:@"viewControllerB" bundle:[NSBundle mainBundle]];
                                            snap.profile = mappingResult.array;
                                            NSLog(@"snap: %@", snap);
                                            [viewController pushViewController:snap animated:YES];


                                        }
                                        failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                            NSLog(@"What do you mean by 'there is no coffee?': %@", error);
                                        }];
}

Exception:

2014-05-25 16:34:00.403 SnapDate[35639:60b] viewcontroller: <snapViewController: 0x10a20ba20>
2014-05-25 16:34:00.403 SnapDate[35639:60b] snap: <snapHomeViewController: 0x10a301c60>
2014-05-25 16:34:00.403 SnapDate[35639:60b] -[snapViewController pushViewController:animated:]: unrecognized selector sent to instance 0x10a20ba20
2014-05-25 16:34:00.405 SnapDate[35639:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[snapViewController pushViewController:animated:]: unrecognized selector sent to instance 0x10a20ba20'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000102371495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001020d099e objc_exception_throw + 43
    2   CoreFoundation                      0x000000010240265d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000102362d8d ___forwarding___ + 973
    4   CoreFoundation                      0x0000000102362938 _CF_forwarding_prep_0 + 120
    5   SnapDate                            0x0000000100003145 __53-[snapApi createNewUser:userPassword:viewController:]_block_invoke + 357
    6   SnapDate                            0x00000001000b69cb __66-[RKObjectRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke244 + 91
    7   libdispatch.dylib                   0x0000000102c6b851 _dispatch_call_block_and_release + 12
    8   libdispatch.dylib                   0x0000000102c7e72d _dispatch_client_callout + 8
    9   libdispatch.dylib                   0x0000000102c6e3fc _dispatch_main_queue_callback_4CF + 354
    10  CoreFoundation                      0x00000001023cf289 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    11  CoreFoundation                      0x000000010231c854 __CFRunLoopRun + 1764
    12  CoreFoundation                      0x000000010231bd83 CFRunLoopRunSpecific + 467
    13  GraphicsServices                    0x00000001033b2f04 GSEventRunModal + 161
    14  UIKit                               0x0000000100c7de33 UIApplicationMain + 1010
    15  SnapDate                            0x0000000100003313 main + 115
    16  libdyld.dylib                       0x0000000102ecf5fd start + 1
    17  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

How can I navigate between ViewControllers programmatically, from the context of the Success?


Solution

  • If you your viewController is inside a UINavigationController then you need to call:

    [viewController.navigationController pushViewController:snap animated:YES]; 
    

    Other wise you cannot display it as push you have to display it modally by calling the following

    [viewController presentModalViewController:snap  animated:YES];