IOS 5.1 UISplitviewcontroller does not allow presentModalViewController from RootViewController
I am using to display the viewcontroller from the RootViewcontroller of a splitview controller using the following code. It will not display in iOS 5.1 portrait mode. when landscape shows controller inside Rootviewcontroller. It works fine until iOS 5.0.
-(void)displayFileInReader:(NSURL *)fileURL {
SPDocumentReader *objiPadDocumentReader = [[SPDocumentReader alloc] init];
objiPadDocumentReader.readerType = ReaderTypeLocalCachedDocument;
objiPadDocumentReader.url = fileURL;
UINavigationController *objNavigationController = [[UINavigationController alloc] initWithRootViewController:objiPadDocumentReader];
objNavigationController.navigationBar.barStyle = UIBarStyleBlack;
[self presentModalViewController:objNavigationController animated:YES];
[objNavigationController release];
objNavigationController = nil;
[objiPadDocumentReader release];
objiPadDocumentReader = nil;
}
I am using self.splitviewcontroller and using a detailviewcontroller instance instead of self.
But in this case, when we click on the detailview controller to remove / remove the rootviewcontroller, it crashes saying something like "no window to view".
Also sometimes in iOS 5.1 the RootView is displayed in the wrong orientation.
early
source to share
Finally I found a solution,
I think the problem might be because the split-view was unable to close the root view controller when we try to present the view controller over it. I changed the above function as follows
-(void)displayFileInReader:(NSURL *)fileURL
{
SPDocumentReader *objiPadDocumentReader = [[SPDocumentReader alloc] init];
objiPadDocumentReader.readerType = ReaderTypeLocalCachedDocument;
objiPadDocumentReader.url = fileURL;
UINavigationController *objNavigationController = [[UINavigationController alloc] initWithRootViewController:objiPadDocumentReader];
objNavigationController.navigationBar.barStyle = UIBarStyleBlack;
//to dismiss the splitview popover -to solve iOS5.1 splitview issue.
[self.delegate dismissSplitViewPopOverController];
[self presentModalViewController:objNavigationController animated:YES];
[objNavigationController release];
objNavigationController = nil;
[objiPadDocumentReader release];
objiPadDocumentReader = nil;
}
Here I have inserted a split-view delegate that allows me to find the split-view popup.
#pragma mark - UISplitView delegate
// Called when rotating to a portrait orientation.
- (void)splitViewController: (UISplitViewController*)svc
willHideViewController:(UIViewController *)aViewController
withBarButtonItem:(UIBarButtonItem*)barButtonItem
forPopoverController: (UIPopoverController*)pc {
barButtonItem.title = SPLocaleString(@"[Navigator]");
self.navigationItem.leftBarButtonItem = barButtonItem;
splitPopoverController = pc;
[splitPopoverController retain];
}
// Called when the view is shown again in the split view, invalidating the button and popover controller.
- (void)splitViewController: (UISplitViewController*)svc
willShowViewController:(UIViewController *)aViewController
invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
self.navigationItem.leftBarButtonItem = nil;
[splitPopoverController release];
splitPopoverController = nil;
}
-(void)splitViewController:(UISplitViewController *)svc
popoverController:(UIPopoverController *)pc
willPresentViewController:(UIViewController *)aViewController
{
if (splitPopoverController)
{
[splitPopoverController release];
splitPopoverController = nil;
}
splitPopoverController = pc;
[splitPopoverController retain];
}
- (void)dismissSplitViewPopOverController {
// If a popover controller is visible, hide it
if (splitPopoverController) {
[splitPopoverController dismissPopoverAnimated:NO];
[splitPopoverController release];
splitPopoverController = nil;
}
}
Thank God,
source to share
I was able to fix this problem in 5.1 thanks to NaveenShan's solution , but the only line I needed was the call [popoverController dismissPopoverAnimated:NO];
before the call presentModalViewController
.
source to share