I hooked into the presented view controller’s init, loadView, viewDidLoad, viewWillAppear:, viewDidAppear:, viewDidUnload and dealloc method to log out relevant timing info.
But it was found that whether presentModalViewController:animated: or presentViewController:animated:completion: will result in two instance of the presented view controller and one of them will soonly be destroyed without anything appeared on screen(loadView and next method hasn’t been fired yet).
Is it a bug or iOS’ view controller transition just works like this?
Here is the log info, WDIMMindItemEditViewController is the presented view controller and WDIMMainScreenViewController is the presenting view controller:
2012-09-29 16:10:02.615 ideaCal[23450:707] < WDIMMindItemEditViewController: 0x2b5260> :editing view controller will initialize
2012-09-29 16:10:02.639 ideaCal[23450:707] < WDIMMindItemEditViewController: 0x2b5260> :editing view controller initialized
2012-09-29 16:10:02.641 ideaCal[23450:707] < WDIMMindItemEditViewController: 0x2b94d0> :editing view controller will initialize
2012-09-29 16:10:02.645 ideaCal[23450:707] < WDIMMindItemEditViewController: 0x2b94d0> :editing view controller initialized
2012-09-29 16:10:02.835 ideaCal[23450:707] < WDIMMainScreenViewController: 0x27c790> will present modal view controller: < WDIMMindItemEditViewController: 0x2b5260>
2012-09-29 16:10:02.841 ideaCal[23450:707] < WDIMMindItemEditViewController: 0x2b5260> :editing view controller load view
2012-09-29 16:10:02.910 ideaCal[23450:707] < WDIMMindItemEditViewController: 0x2b5260> :editing
view controller has loaded view2012-09-29 16:10:02.912 ideaCal[23450:707] < WDIMMindItemEditViewController: 0x2b5260> :editing view controller’s view will appear
2012-09-29 16:10:03.297 ideaCal[23450:707] < WDIMMainScreenViewController: 0x27c790> will present modal view controller: < WDIMMindItemEditViewController: 0x2b94d0>
2012-09-29 16:10:03.302 ideaCal[23450:707] < WDIMMindItemEditViewController: 0x2b94d0> :editing view controller will dealloc
2012-09-29 16:10:03.340 ideaCal[23450:707] < WDIMMindItemEditViewController: 0x2b94d0> :editing view controller ended dealloc
2012-09-29 16:10:03.702 ideaCal[23450:707] < WDIMMindItemEditViewController: 0x2b5260> :editing view controller’s view appeared
2012-09-29 16:10:05.434 ideaCal[23450:707] < WDIMMainScreenViewController: 0x27c790> ended present modal view controller: < WDIMMindItemEditViewController: 0x2b5260>
And here is my presenting code:
- (IBAction)slidingMenuGetPressed:(WZUICircularPagingControlViewSlidingMenu *)slidingMenu event:(UIEvent *)event
{
WDIMEditViewController * editModalViewController = nil;
if ([slidingMenu.identifier isEqualToString:NEW_MIND_ITEM]) {
editModalViewController = [[WDIMMindItemEditViewController alloc] init];
}
if ([slidingMenu.identifier isEqualToString:NEW_PROJECT]) {
editModalViewController = [[WDIMMindItemEditViewController alloc] init];
}
if ([slidingMenu.identifier isEqualToString:NEW_TAG]) {
editModalViewController = [[WDIMTagEditViewController alloc] init];
}
dispatch_queue_t snapshotQueue = dispatch_queue_create("com.WeZZardDesign.ScreenSnapshotQueue", NULL);
dispatch_async(snapshotQueue, ^{
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * snapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
editModalViewController.backgroundImage = snapshot;
NSManagedObjectContext * workingContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
workingContext.parentContext = self.database.managedObjectContext;
editModalViewController.preference = self.preference;
editModalViewController.dataSource = self;
editModalViewController.delegate = self;
editModalViewController.workingContext = workingContext;
NSLog(@"%@ will present modal view controller: %@", self, editModalViewController);
[self presentViewController:editModalViewController animated:YES completion:^{
NSLog(@"%@ ended present modal view controller: %@", self, editModalViewController);
}];
});
});
dispatch_release(snapshotQueue);
}
And I have got nothing changed in presentModalViewController:animated: and presentViewController:animated:completion: method.
From the comments above it has become clear that
NEW_MIND_ITEMandNEW_PROJECTare two strings having the same contents. SinceisEqualToStringcompares the contents, not the pointer, two “if” blocks are executed, creating two instances ofWDIMMindItemEditViewController. Using different strings should solve the problem.