2016-05-27 11 views
2

ich diesen link gefolgt haben die compleltionblockGriff ContainerViewController Handeln in parent

@interface : ParentViewController() 

@property (nonatomic, strong) ChildViewController *childViewController; 

@end 

In Eltern-View-Controller des Tabellenansicht Delegate-Methode (DidSelectRowAtIndexPath) Ich addiere Container Ansicht zu implementieren, wie

- (void) addChildView { 
    ChildViewController * childViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ChildViewController"]; 
    childViewController.view.frame = (CGRect) {0, 0, self.view.frame.size}; 
    [self addChildViewController:childViewController]; 

    __block ParentViewController *parentViewController = self; 
    [parentViewController.childViewController setCompletionCallBack:^{ 
     self.childViewController.view.backgroundColor = [UIColor clearColor]; 
     [UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{ 
      self.childViewController.view.frame = (CGRect) {0, 1000, self.view.frame.size}; 
     } completion:^(BOOL finished) { 
      [self.childViewController.view removeFromSuperview]; 
      self.childViewController = nil; 
     }]; 
    }]; 


    [self.view addSubview: childViewController.view]; 
    [childViewController didMoveToParentViewController:self]; 

    [UIView animateWithDuration:0.5f animations:^{ 
     childViewController.view.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.15f]; 
     [self.view layoutIfNeeded]; 
    } completion:nil]; 
} 

ChildViewController der folgt Schnittstelle und Implementierung

@interface ChildViewController : UIViewController 

@property (nonatomic, copy) void (^completionCallBack)(); 

@end 

- (IBAction)cancelPressed:(id)sender { 
    self.completionCallBack(); 
} 

wenn ich versuche, die completionCallBack innerhalb der Schaltfläche Aktion Ich bekomme einen schlechten Zugriffsfehler. enter image description here

Ich bin mir nicht sicher, welchen Fehler ich hier mache, jede Hilfe sehr geschätzt.

Antwort

0

Endlich fand ich den Grund für den schlechten Zugriffsfehler. Eigentlich habe ich die Instanz von ChildViewController als globale Variable und ich lade nicht den ChildViewController in dieser globalen Variable. Wenn ich versuche, die completionCallBack ChildViewController-Instanz aufzurufen, dann ist nil das der Grund für den Fehler.

Fix - ChildViewController * childViewController

self.childViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ChildViewController"]; 
     childViewController.view.frame = (CGRect) {0, 0, self.view.frame.size}; 
     [self addChildViewController:childViewController];