Search code examples
iosobjective-cuitextfielddelegate

setting Delegates on destination viewController


This is puzzling me.

The context

The original tutorial I'm following.

enter image description here

Where the segue is added to the Main View via a custom segue:

- (void) perform {
    
    MainViewController *source = (MainViewController *)self.sourceViewController;
    UIViewController *destination = (UIViewController *) self.destinationViewController;
    
    for(UIView *view in source.main.subviews){
        [view removeFromSuperview];
    }
    
    source.currentViewController = destination;
    destination.view.frame = CGRectMake(0, 0, source.main.frame.size.width, source.main.frame.size.height);
    [source.main addSubview:destination.view];
}

The TextField is connected as delegate in the child View Controller. All things being equal I get the app crashed without any message.


The workaround

In the Main View Controller, in -(void)prepareForSegue: I've added [segue.destinationViewController setDelegate:self]; in the meantime I've added a property in the child View Controller id<UITextFieldDelegate> delegate and modified the textfield delegate as self.delegate.

This works, but the trouble is that I've to set the delegated methods in Main View Controller which is not quite efficient as I have more View Controllers to add.


The Objective

How do I set each View Controller to be the delegate for itself without crashing?


Solution

  • At first, let's see crash report. Please, do the following:
    1. Add Exception Breakpoint
    2. Edit it as in the picture

    enter image description here