Reputation:
I have a RootViewController that presents FirstViewController with the instance method presentViewController:animated:completion:. I have a log message ("Presenting") in the method; it appears so I know my root is being called on in my FirstViewController, but for some reason the message gets logged continuously before the program crashes.
RootViewController
-(void)presentViewController:(UIViewController *)viewControllerToPresent
animated:(BOOL)flag
completion:(void (^)(void))completion {
NSLog(@"Presenting");
[self presentViewController:viewControllerToPresent animated:flag completion:completion];
}
FirstViewController (where I call on the method)
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
MyRootController *root = [[MyRootController alloc] init];
[root presentViewController:self animated:YES completion:nil];
NSLog(@"asdhflkajshdfl %@...", [self presentingViewController]);
}
I'm not sure how I can have the method be executed only once. This is my first time using the method, but I assumed that the message would be logged only once.
Upvotes: 0
Views: 1247
Reputation: 4745
I suspect you want to know, when presentViewController...
is called (EDIT: ... , because there's only the log call and the call to the same method in it). You also probably want to call the super class method, so you need to use super
instead of self
:
-(void)presentViewController:(UIViewController *)viewControllerToPresent
animated:(BOOL)flag
completion:(void (^)(void))completion {
NSLog(@"Presenting");
// Note super here:
[super presentViewController:viewControllerToPresent animated:flag completion:completion];
}
In your original code, you are calling the same method from within itself. This leads to the many log messages.
Upvotes: 1