I have an error which is causing my app to crash under iOS5 only on the iPad.
The below code is called when the user taps on an item in a uibarbutton item :
- (void)optionSelected:(NSString *)option {
[self.optionPickerPopover dismissPopoverAnimated:YES];
if ([option compare:#"Map View"] == NSOrderedSame) {
NSLog(#"Map View");
MapView * map = [[MapView alloc] initWithNibName:#"MapView" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:map];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self action:#selector(removeCurrent)];
map.navigationItem.rightBarButtonItem = rightButton;
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[map release];
[rightButton release];
[split presentModalViewController:map animated:YES];
}
Can anyone suggest why this occurring in iOS5 ?
You are getting this error because you are attempting to display the 'map' view controller twice. The first time is as the root view controller of 'navigationController' and the second time is via [split presentModalViewController:map animated:YES].
iOS 5 is being a bit more picky than iOS 4 when you try to do strange things with view controllers. Trying to show the same controller twice is a design problem - you need to work out what you are really trying to do and fix it.
(Also, calling a map view controller 'MapView' rather than 'MapViewController' is really confusing)
This error will also occur if you don't follow these guidelines: Creating Custom Content View Controllers
Basically, you need to call:
[yourVC removeFromParentViewController];
if you've
[parentVC addChildViewController:yourVC];
This error may often be paired with something about "UIViewControllerHierarchyInconsistency"
Related
How can i display the background of the NavigationController (including title/buttons) in my UIPopovercontroller on the iPhone?
At the moment it looks like that:
In my PopoverView-ViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.hidden = NO;
self.navigationController.navigationBarHidden = NO;
self.navigationItem.title = #"self.navigationItem.title";
}
When calling Popover:
InfoView *iv = [[InfoView alloc] initWithNibName:#"InfoView" bundle:nil];
UINavigationController *uc = [[UINavigationController alloc] initWithRootViewController:iv];
self.pop = [[UIPopoverController alloc] initWithContentViewController:uc];
[self.pop setDelegate:self];
[self.pop presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:true];
It should look like this:
At first UINavigationController doesn't have own background, it shows background of content view controller. If you can show popover with navigation controller successfully, then check your InfoView. Does it's view has some background?
Second, I tried to execute your code multiple times in different configurations on iPhone, but always have received error
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[UIPopoverController
initWithContentViewController:] called when not running under
UIUserInterfaceIdiomPad.
And that was not surprise for me. Even if you could make it work, then Apple may reject your application.
2.1 Apps that crash will be rejected - Just test your code on several devices and see result.
your code
InfoView *iv = [[InfoView alloc] initWithNibName:#"InfoView" bundle:nil];
UINavigationController *uc = [[UINavigationController alloc] initWithRootViewController:iv];
self.pop = [[UIPopoverController alloc] initWithContentViewController:uc];
[self.pop setDelegate:self];
[self.pop presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:true];
is perfect just hide the default navigation controller and add a navigationBar in the XIB of InfoView.xib and write the cancelButtonAction there to perform the dismissal of the view or the detail of the tableview selection. It Works fine, Please check the image i have done the same way, and believe me it works fine.
Add this to your code before the line [self.pop presentPopoverFromBarButtonItem:se..:
uc.modalPresentationStyle = UIModalPresentationCurrentContext;
See the documentation for the modalPresentationStyle property.
I think you'll need to paste in more of the code for your InfoView controller, as I suspect that's where the problem is.
Have you tried setting up your background view as a class?
[pop setPopoverBackgroundViewClass:bgView];
I have looked at several tutorials and different posts and can't seem to figure this out. I am simply trying to create a back button to take me back to a view where I was before. In the IB I create the button and link it to my backButton method. However, when I try to use it in the App I can't get it to work. Do I have to push it on the stack first? Any suggestions?
-(void)backButton:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
Here is the code where I navigate from one view into another: Map is a view I use.
- (void) buttonClicked:(id)sender
{
UIButton *selectedButton = (UIButton *)sender;
int tempButtonTag = selectedButton.tag;
Map *map =[[Map alloc] initWithNibName:nil bundle:nil];
NSMutableString *tempID = [buttonIDArray objectAtIndex:tempButtonTag];
NSMutableString *tempType = [buttonTypeArray objectAtIndex:tempButtonTag];
[map setXmlID:tempID];
[map setXmlType:tempType];
buttonIDArray = nil;
buttonTypeArray = nil;
[buttonIDArray release];
[buttonTypeArray release];
[self presentModalViewController:map animated:YES];
}
The opposite of presentModalViewController is dismissModalViewController.
The opposite of pushViewController is popViewController.
You are mixing these up...
I have password authentication for my application. My application is window based. I have five view: MyApplicationAppDelegate,MainViewController, HelpViewController, SettingViewController and ErrorViewController.
When the application starts, I navigate to MainViewController. I have two buttons on toolbar for Help and Setting which direct to respective pages. Now, I have written following code in MyApplicationAppDelegate:
- (void)applicationWillEnterForegroundUIApplication *)application {
[self showMyView];
}
-(void)showMyView{
if (![Global timeSet]) {
if (errorviewFlag) {
if (![Global show]){
ErrorPageViewController *bViewController = [[[ErrorPageViewController alloc]
initWithNibName"ErrorPageViewController" bundle:[NSBundle mainBundle]] autorelease];
[self setError:bViewController];
self.error = bViewController;
//[window addSubview:[error view]];
[[self navigationController] pushViewController:bViewController animated: NO];
UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle"Back" style:UIBarButtonItemStyleDone target:nil action:nil];
[self.navigationController setNavigationBarHidden:YES];
[self.navigationController setToolbarHidden:YES];
[_backButton release], _backButton = nil;
[window makeKeyAndVisible];
errorviewFlag=FALSE;
[Global setShow:TRUE];
}
}
}
}
Now, this method is called for all times except the first.
The problem is this only works in desired way, i.e. shows ErrorPage, when I minimize the application from MainView. If I minimize from Help or setting, it does not show the error page, but instead the mainpage with no navigation controls. Why?
He was able to find the answer. I used NSNotificationcenter with didEnterBackground. I used this in all other view than MainView. And in my didEnterBackground method i used
[self.navigationController popViewControllerAnimated:NO];
This forced all other view to come to MainView when application is minised and this solved the problem.
In my app, I'm presenting a modalviewcontroller as follows and I'm not able to change the navigationbar's title or any of its properties for that matter.
fullListTopCompanies *fullListTopCompaniesInstance = [[fullListTopCompanies alloc] initWithNibName:#"fullListTopCompanies" bundle:nil];
UINavigationController *cntrol = [[UINavigationController alloc] initWithRootViewController:fullListTopCompaniesInstance];
[fullListTopCompaniesInstance setTitle:#"TEST"];
UIBarButtonItem *submit = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(displayViewForPosts)];
fullListTopCompaniesInstance.navigationItem.rightBarButtonItem = submit;
[submit release];
[self presentModalViewController:cntrol animated:YES];
[cntrol release];
I tried instantiating application delegate and assigning its navigationcontroller to local navigationcontroller instance but no use.
Somehow that navigationcontroller is not accessible. It can't be accessed by using "self.navigationitem". Whenever I present modalviewcontroller with the navigationcontroller, this navigation comes below the actual navigationcontroller.
For Example, if you are trying to set title of navigation bar for the ViewController called "ABCViewController", then add
self.Title = #"";
in viewWillAppear Method of the ABCViewController and try to rebuild and Run.
Hope this helps. :)
Whenever I present modalviewcontroller with the navigationcontroller, this navigation comes below the actual navigationcontroller.
That problem is because calling presentModalViewController: on self, you should call it on self.navigationController that way the navigation controller won't be shown below the other one.
As to why you can't set the navigationController's properties, I don't know. It looks Ok to me. But I expect it is because you are setting the properties before viewDidLoad is called by the nib-loader. I think I remember having problems like this myself a long time ago.
You should set the title etc. in the UIViewController subclass's viewDidLoad method and I think you worries will be over.
I've created a simple view based app with xcode template, then i've added your code and it's working for me...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
TestViewController *fullListTopCompaniesInstance = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
UINavigationController *cntrol = [[UINavigationController alloc] initWithRootViewController:fullListTopCompaniesInstance];
[fullListTopCompaniesInstance setTitle:#"TEST"];
UIBarButtonItem *submit = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(displayViewForPosts)];
fullListTopCompaniesInstance.navigationItem.rightBarButtonItem = submit;
[window addSubview:viewController.view];
[window makeKeyAndVisible];
[viewController presentModalViewController:cntrol animated:YES];
[cntrol release];
[submit release];
return YES;
}
Hi i have a splitViewController
mapViewController = [[MapViewController alloc] initWithManagedObjectContext:managedObjectContext startingRegion:startingRegion];
distanceViewController = [[DistanceTableViewController alloc] initWithManagedObjectContext:managedObjectContext];
distanceViewController.mapViewController = mapViewController;
setupViewController = [[SetupTableViewController alloc] initWithStyle:UITableViewStyleGrouped map:mapViewController.map];
setupViewController.positionSwitch.on = savePosition;
SearchTableViewController *searchViewController = [[SearchTableViewController alloc] initWithStyle:UITableViewStylePlain managedObjectContext:managedObjectContext];
searchViewController.mapViewController = mapViewController;
tabBarController = [[UITabBarController alloc] init];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
UINavigationController *mapNavigationController = [[[UINavigationController alloc] initWithRootViewController:mapViewController] autorelease];
UINavigationController *searchNavigationController = [[[UINavigationController alloc] initWithRootViewController:searchViewController] autorelease];
UINavigationController *distanceNavigationController = [[[UINavigationController alloc] initWithRootViewController:distanceViewController] autorelease];
UINavigationController *setupNavigationController = [[[UINavigationController alloc] initWithRootViewController:setupViewController] autorelease];
UISplitViewController* splitVC = [[UISplitViewController alloc] init];
splitVC.viewControllers = [NSArray arrayWithObjects:searchNavigationController, mapNavigationController, nil];
splitVC.title = #"iMetano";
splitVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:#"Mappa" image:[UIImage imageNamed:#"mapIcon2.png"] tag:0] autorelease];
NSArray *viewControllersArray = [NSArray arrayWithObjects: splitVC,setupNavigationController,nil];
[splitVC release];
tabBarController.viewControllers = viewControllersArray;
}
When i startup my app in portrait, all works fine.
When i startup my app in landscape this is the result
I see only the view of the first viewController SearchTableViewController with some pixel between the UINavigationController and the status bar
When i rotate in portrait and after i return in landscape i see both viewController's view, but the second have some pixel between the statusBar and the UINavigationControllor
I can't understand why.
apple says not to put a split view controller inside something else, like a tab bar controller
After looking at my code and IB time after time. This is the best that I could come up with. Not sure if is the best one but it works for me. Im loading a default detail view controller. If I load the controller directly in the viewDidLoad then the problem occur. If I load it from the selector the problem goes away. I hope this helps. I have this code in the RootViewController.
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:#selector(loadController) withObject:nil afterDelay:0];
}
-(void)loadController{
UIViewController <SubstitutableDetailViewController> *detailViewController = nil;
WebViewController *newDetailViewController = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:nil];
[newDetailViewController setTitle:#"Home"];
NewNavController <SubstitutableDetailViewController>*navController = [[NewNavController alloc] initWithRootViewController:newDetailViewController];
detailViewController = navController;
NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = viewControllers;
}
I had this exact same problem when attempting the combination of tab bar, split view and navigation controllers. I noticed that the alignment gap is only present when the application first fires up and the first tab is auto-selected because it's the first tab in the tab bar controller's array of view controllers. After switching tabs and then coming back to the one with the misaligned nav controller in a split view, there was no alignment problem present. So, to replicate this behavior and get rid of the misalignment when the screen is first rendered I added:
[tabBarController setSelectedViewController:splitVC];
right after setting the view controller array on the tab bar controller. Works like a champ now.
I know this is an old question, but here's the hack I just used to get around this problem for anyone who has a navigation hierarchy like mine:
UITabBarController
Tab0->UINavigationController->MGSplitViewController _or_ UISplitViewController
Tab1->UINavigationController->SomeOtherViewController
Tab2->Etc...
Nothing I tried could get rid of that 20px gap that occurs only once, at bootup, if the device orientation is anything except UIInterfaceOrientationPortrait. The 20px gap is caused by the UINavigationBar for the split view's UINavigationController above having a non-zero origin.y value; most likely, you'll find it to be 20.
Also, I found that this is only a problem if the device is running iOS < 5.0.
I check for this issue in the view controller code of my MGSplitViewController (i.e. self = an MGSplitViewController):
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if(self.doIOS4OneTimeRotationHack == YES)
{
self.doIOS4OneTimeRotationHack = NO;
for(UINavigationController *navController in [self viewControllers])
{
if(navController.navigationBar.frame.origin.y != 0.0f)
{
[UIView animateWithDuration:0.01
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:
^(void)
{
navController.navigationBar.frame = CGRectMake(navController.navigationBar.frame.origin.x,0.0f, navController.navigationBar.frame.size.width,navController.navigationBar.frame.size.height);
}
completion:
^(BOOL finished)
{
//NSLog(#"Shifted navbar 0x%x up!",navController.navigationBar);
}];
}
}
}
}
With the animation set to finish in just 0.01 seconds, it happens so fast that you'll never even notice it as your bootup splash screen disappears and your MGSplitViewController view appears in its place. Maybe play around with it and make it instantaneous; I had to get it working and move onto my next task, so I didn't fool with it past that point.
I don't like resorting to hacks like this, but this was the only way I was able to get around this problem. ScottS' solution below sounded great, but unfortunately didn't work for me.