2016-05-27 1 views
2

J'ai suivi ce link pour mettre en œuvre la compleltionblockPoignée action de ContainerViewController dans ParentViewController

@interface : ParentViewController() 

@property (nonatomic, strong) ChildViewController *childViewController; 

@end 

Dans Parent Voir la méthode du contrôleur Tableau délégué (didSelectRowAtIndexPath) J'ajoute vue du conteneur comme suit

- (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 de Interface et implémentation

@interface ChildViewController : UIViewController 

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

@end 

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

lorsque j'essaie d'appeler le completionCallBack à l'intérieur de l'action du bouton, j'obtiens une erreur d'accès incorrect. Je ne suis pas sûr de ce que je fais erreur, Toute aide grandement appréciée.

Répondre

0

Enfin, j'ai trouvé le motif de l'erreur d'accès incorrect. En fait j'ai l'instance de ChildViewController comme variable globale et je ne charge pas le ChildViewController dans cette variable globale ainsi quand j'essaye d'appeler l'instance de completionCallBack ChildViewController est nulle c'est la raison de l'erreur.

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];