Add a UINavigationController in CCLayer - uinavigationcontroller

I am trying to create an app that uses cocos2d for the homescreen, and a UINavigationController for some tables and information. I use this code to try to push a UINavigationController (Settings) into view
UINavigationController *controller = [[UINavigationController alloc] init];
Settings *newTableViewController = [[Settings alloc] init];
[controller pushViewController:newTableViewController animated:YES];
This code is executed in a CCLayer, and Settings is a UINavigationController.
I get this error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'
Does this mean that I can't push a NaviagationController into a CCLayer?
Please Help.

You cannot "push view controller to the CCLayer". All that you are doing using cocos2d takes place inside one OpenGL view.
If you really want to do something like this, you can change creation of the application's root view controller. Make your application view controller a subclass of UINavigationController instead of UIViewController, then just get rootViewController from your app delegate and push/pop any other view controller to it whenever you want. Anyway, I don't think, that it is a good idea.
About your exception I assume that your Settings class is a subclass of UINavigationController. All the controllers that you want to push to the navigation controller must be derived from UIViewController, not UINavigationController.

Related

UIBarButtonItems shift position when UINavigationController is presented modally

I'm presenting a UINavigationController modally, from within an iOS app extension:
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
nav.modalPresentationStyle = UIModalPresentationFormSheet;
nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:nav animated:YES completion:nil];
When the navigation controller appears, its root view controller's UIBarButtonItems jump position:
I'm creating and adding the buttons in viewDidLoad. They are just standard bar button items:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done)];
I'm not overriding viewDidAppear (which appears to be the point where the buttons jump).
Presenting this same navigation controller/root view controller from within my app, instead of the app extension, doesn't give me this same problem. Any ideas?
I'm hoping someone else finds a better way to do this, but my current solution is to create stand alone buttons and then use [[UIBarButtonItem alloc] initWithCustomView:] with the button. Not ideal, and the indention is still there, but the weird jump doesn't happen anymore.
If the indention bothers you, you could also set negative fixed spaces on the outsides of the buttons to shift them closer to the edges.
Here's an example including the spacer:
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
<other button config here, set targets, whatever>
[button sizeToFit];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpace setWidth:-8.0f];
self.navigationItem.leftBarButtonItems = #[fixedSpace, leftBarButton];
This only happens to me on my share extension, and I don't have any problems related to presenting from a detached controller or anything like that.
As #Stonz2 mentioned, this appears to be a characteristic of presenting a modal from a detached view controller. I was having the same problem and remedied it by re-organizing my app so that it wasn't presenting from a detached controller.
You'll know if you're presenting from a detached controller if you get the following error message:
Presenting view controllers on detached view controllers is discouraged
I was presenting from a detached viewController. I never got the warning. But I tried embedding that detached viewController in a NavigationController, and the problem went away.

Linking Storyboards Without UINavigationController

I have an iOS 6 app that I am updating to work with iOS 7 and to make it use storyboards. I am trying to use multiple storyboards so that I can break my project down into modules for each screen in the app. So far this has worked out fine but now I need to provide a way to navigate between the various storyboards while still making the work like it did in iOS 6 (but with updated artwork).
I don't use UINavigationController in my existing iOS 6 app and I would prefer not to use it as up to now I have been able to navigate back and forth between XIB's using code on UIButton tap gestures. The UINavigationController doesn't make it easy to customise how the navigation buttons look from what I have learned so far about it.
I found this very clean way of moving between view controllers that are on different storyboards https://github.com/rob-brown/RBStoryboardLink by passing the name of the storyboard in as an attribute.
But it only seems to work when UINavigationController is used. I get an error "Push segues can only be used when the source controller is managed by an instance of UINavigationController" without UINavigationController.
Is there a way to navigate between storyboards by only using the above RBStoryboardlink but without the need for UINavigationController?
Push segues can only be used when the source controller is managed by an instance of UINavigationController
This means you are trying to push a view controller to the source while source don't have any navigation controller stack.In that case, you should try to add the instantiated view controller's view as subview to the source view controller's view.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController = [mainStoryboard instantiateInitialViewController];
[self.view addSubview:viewController.view];
or you modally present, that purely depends on your requirement.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController = [mainStoryboard instantiateInitialViewController];
tabBarViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:tabBarViewController animated:NO completion:NULL];

Why is self.navigationController = nil from a method

For some unknown reason I cannot push a view, I will try to explain the best I can but i have alot of complicated views going on, And it would be a nightmare to explain but say I have the following method.
-(void)showDetailView{
DetailViewController *detailView = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
[self.navigationController pushViewController:detailView animated:YES];
[detailView release];
}
Which works and would push the detail view onto the stack, this is on my main view and my main thread.
So in the table view i have a cell with a Subview that then takes its view from another view controller. In that view controller I have more subviews, and say when a user clicks a subview a method is called. Which in turn will call this method on the mainView.
So one would believe that then a new view should get pushed. And the code runs, no errors occur but the view is not changed/switched.
I have tried various method of pushing a view inside the view controller that is it called from. and then just calling a method which is directly connected to the navigation controller to push the views.
A few things to add.
1. I have a IBAaction button that pushes a view (that works fine)
2. I assume its because im through so many views but I'm assuming that if you call push view controller it will push whatever view you pass it.
3. I have checked when the method is called from the main view self.navigationcontroller does not = null.
But if the navigation controller is called through the method call it = null.
So is there anyway to restore the null value of a navigation controller?
Im a little confused on why i cant simply call a method to push a view
Thanks
Its ok I have sorted it now
In terms of how i solved accessing the superior superview to my view is
An ID tag to the views superview which is the cell that my subview is in
id CellController = [self.view.superview.superview nextResponder];
[CellController performSelector:#selector(showDetailView:) withObject:Link];
Then calling a method in my cell which then in turn access its superview
id mainController = [self.view.superview.superview nextResponder];
[mainController performSelector:#selector(showDetailView:) withObject:Link];
Which then the main still retains its navigation controller and then in the show detail method in the main passes the link and pushes the new view.
Basically the total superview of a subview can be no higher then wherever it was created. So the subview in my cell could never access and run anything through my mainView properly, i could stuff but it never performed how it should be. So accessing its highest level superview and then getting that to access its superview as the cell is in my main view then I could run methods correctly.
Hope this makes sense only getting my head around it all.
Thanks to -> How does one access a super's view controller?
For the Id tag bit, not used Id in this way so another things learnt
I just had nearly the same problem today. If the navigation bar is hidden when you push a view, you may have a nil navigation controller. I resolve the problem like this :
self.navigationController.navigationBarHidden = NO; // So the navigation controller is not nil
[self.navigationController pushViewController:articleInfoController animated:YES];

How do I get toolbars on UISplitView detail & root views?

I'm porting my iPhone app to iPad. I have a bunch of detail views that derive from UIViewController and implement UITableViewDelegate. (Basically TableViews but not derived as such). The old app used TabBar, but now I'd like to use SplitView with toolbars on the Detail and Master views. How do I enable a toolbar on my UIViewController inside a UISplitViewController? I'm not using Interface Builder here, just code.
#interface HeadlineViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *tableView;
NSMutableArray *bullIds;
UIActivityIndicatorView *prog;
}
Thanks,

viewDidAppear Not Firing in Tabbarcontroller>NavigationController>UITableView

I currently have a tab bar controller set up with a navigationcontroller on one of the tabs, then I have a UITableView nib set up for that Navigationcontrollers view. All of this has been set up through IB and I want to keep it that way. Kind of like this tutorial http://twilloapp.blogspot.com/2009/05/how-to-embed-navigation-controller.html
now the view loads perfectly when ViewDidLoad is called. But when I then load further views via code IE
MyApp_AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
UINavigationController *nav = (UINavigationController *)delegate.controller.selectedViewController;
newViewController = [[newViewController alloc] initWithNibName:#"newView" bundle:nil];
[nav pushViewController:newViewController animated:YES];
//At this point the view works! and loads
If I try to go back with the navigation toolbar it goes back to my previous view fine
Now I need to refresh the tableview when I go back but calling viewDidAppear does not work.
I tried adding UINavigationDelegate to the same ViewController Class as the tableview and then calling - (void)navigationController:(UINavigationController *)navigationController didShowViewController:
But that did not work. I also tried adding the same delegate as the tab bar controller and adding the same navigationController didShowViewController: there but that also failed.
How do I get this table to refresh every time the view loads?
You should not have to call viewDidAppear from your code. Cocoa Touch should do that for you.
Call the table view's reloadData method to get it to refresh its contents.
Found I was missing the Delegate declaration in the Interface file. doh! also I tried that in lots of places it only ended up working when I added it to the NavigationControllers first view (my table view)

Resources