Search code examples
iosiphonedelegatesmodalviewcontrollerpresentmodalviewcontroller

nil delegate in modal view after presentViewController.


My first view in the viewDidLoad has the following code

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Dialpad" bundle:nil];
self.vc = [sb instantiateViewControllerWithIdentifier:@"DialpadBoard"];
self.vc.delegate = self;

The header file contains the definition of the view controller

@property (nonatomic, retain) DialpadTableViewController *vc;

after catching an event the view loads a new modal view

- (void)handleEvent:(UIGestureRecognizer*)recognizer {
    [self presentViewController:self.vc animated:YES completion:NULL];
}

The view also contains the method to dismiss the modal view:

- (void) dialpadControllerDidCancel:(SearchDialpadTableViewController *)controller {
     [self dismissViewControllerAnimated:YES completion:nil];
}

The last method never gets called.

The problem is that the modal view when it is loaded has nil self.delegate. The new modal view is loaded from the storyboard as seen below. Why the delegate is nil? I cannot how a segue since the view is in another storyboard.
enter image description here


Solution

  • you can try this

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Dialpad" bundle:nil]; 
    UINavigationController *nav= [sb instantiateViewControllerWithIdentifier:@"DialpadBoard"];
    
    
    
    
    ((SearchDialpadTableViewController *)[nav viewControllers][0]).delegate = self; 
    [self presentViewController:nav animated:YES completion:NULL];
    

    Since we have the nav let's take the first controller and assign the delegate

    the trick ;)

    ((SearchDialpadTableViewController *)[nav viewControllers][0]).delegate = self;