Difference in ViewController pushing - ios

I'm doing the CS193P Stanford course tutorials and also some Apple iOS dev tutorials, and there's a difference between how they push the ViewController to the screen
Apple does this:
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;
Stanford suggests doing this:
navigationController = [[UINavigationController alloc] init];
[self.navigationController pushViewController:rootViewController animated:NO];
How are they different?
PS: btw, Apple's method work and the Stanford one doesn't display anything and I don't know why.

I think that using the pushViewController:animated method you are going to add the controller at the top of the stack of the controllers (push a new view controller on the stack).
In the second method you are not initializing the navigationController.

Related

Presenting view controllers on detached view controllers is discouraged - definition of "detached view controller"?

OK. So I've looked around the web and read similar questions about this relatively new iOS warning. My app uses NO storyboard. I only have one simple question. What is a "detached view controller" and can anyone provide a definitive reference (e.g. to an Apple doc) that provides a definition of "detached view controller" as we are to understand it in the context of this warning. I fail to see how other answers to this question are more than fumbles and guesses around the topic if no-one truly understands precisely what a detached view controller is.
A detached view controller is one that is not currently in the hierarchy of [[[UIApplication sharedApplication] keyWindow] rootViewController]
For example, I could instantiate a UIViewController anywhere in my code, but if I never put it in this hierarchy by making it my rootViewController or pushing onto a navigation stack in my rootViewController, then I would get strange/undefined behavior when I present something on it.
Example:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIViewController* a = [[UIViewController alloc] init];
UIViewController* b = [[UIViewController alloc] init];
UIViewController* someOtherVC = [[UITableViewController alloc] init];
self.window.rootViewController = a;
//Works fine :)
[a presentViewController:someOtherVC animated:YES completion:nil];
//might break the world
[b presentViewController:someOtherVC animated:YES completion:nil];
}

Accessing a Storyboard ViewController from AppDelegate issue

I am trying to access an Array variable in a view controller in an application that is using storyboards.
BACKGROUND:
I have been following along with the Ray Wenderlich tutorial on Storyboards.
Once I finished the tutorial, I went back tried a different route, though I’m having trouble accessing a view controller. Everything is pretty much the same except my set up is the initial Scene is a View Controller. I am to the part where the author is adding some data to NSMutableArray in his table.
THEIR CODE THAT I AM USING AS A GUIDE
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = [tabBarController viewControllers][0];
PlayersViewController *playersViewController = [navigationController viewControllers][0];
playersViewController.players = _players;
I was hoping it would be a simple as replicating what I had seen with view controllers, passing along the appropriate type, but no.
I have View Controller > View Controller > Navigation View Controller > UITableViewController.
MY CODE:
UIViewController *vc = (UIViewController*)self.window.rootViewController;
UIViewController vc1 = [vc viewController][0];
UINavigationController *nc = [vc1 viewController][0];
SearchViewTableViewController *svc = [nc viewControllers][0];
svc.myarray = _myarray;
I have tried multiple combinations and am getting nowhere.
There has got to be a simpler way for me to reference classes/view/scenes.
Any help?
Make sure you are importing the ViewController header file.
Make sure you have given you ViewController a Storyboard identifier.
Then something like this should work:
MyViewController *myVC = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"MyViewController"];
myVC.myMutableArray = [NSMutableArray new];
....

ios - Nothing happens when pushing view controller to navigation controller

I am trying to push a view controller to a navigation controller, but nothing happens.
I have the following code in my appDelegate (which works fine it seems):
ViewController1* VC1 = [[ViewController1 alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:VC1];
self.window.rootViewController = navController;
And the following code in VC1:
ViewController2 *VC2 = [[ViewController2 alloc]init];
[self.navigationController pushViewController:VC2 animated:YES];
VC2 gets initialised but isn't pushed to the navigation controller and therefor never presented. I have tried looking for answers for a while now but without success. What am I missing?
Thanks in advance!
Turns out the problem was that another navigation controller was active and had to be dismissed. The code posted was correct and all but I had totally missed another navigation controller.

How to show the Navigation Bar in an iOS view?

I'm opening a View from the navigationController, using the NVSlideMenuController. However, I haven't been able to show a Navigation Bar (which I definitely need).
I'm not familiar with NavigationControllers and after following a few tutorials, it still isn't clear enough to me how it works.
This is in the AppDelegate application didFinishLaunching:
IntroViewController* introVC = [[IntroViewController alloc] initWithNibName:#"IntroViewController" bundle:nil];
UIViewController *menuViewController = [[MenuViewController alloc] initWithStyle:UITableViewStyleGrouped]; // Your menu view controller
UIViewController *contentViewController = (UIViewController*)introVC; // The initial content view controller
menuViewController.navigationController.navigationBarHidden = false;
NVSlideMenuController *slideMenuController = [[NVSlideMenuController alloc] initWithMenuViewController:menuViewController andContentViewController:contentViewController];
self.window.rootViewController = slideMenuController;
return YES;
I tried adding the code to put the navbarhidden to false and it doesn't seem to work. Is there something else I missed?
Any help is very appreciated!
MyViewController *myViewController = [[MyViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
self.window.rootViewController = navigationController;
This will set the UINavigationController as your root view controller. If you MUST use NVSlideMenuController (which I have 0 experience with but really don't think it is necessary), then you can do the first two lines I gave you, and set the navigationController as the root for the NCSlideMenuController.
I would recommend Apple's documentation for UINavigationController, it is an extremely useful thing to know:
https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html

Layout best practices

I am new to Objective-C and developing a simple information app. Each screen simply links to another without any real programming for now. As I add new View Controllers for each screen, I realize I will have a large number of View Controllers for this app.
My question: Is this the best way to handle a simple app like this? There are 5 main sections, each section contains 3-5 sub sections, which would result in many View Controllers. I am thinking there is a simpler, cleaner way to maneuver text dynamically within one View Controller. Any ideas?
You can use a tabBar controller with 5 tabs. In each tab there is a navigation controller that manages other view controllers of this section:
myTabBarController = [[UITabBarController alloc] init];
UIViewController *vc1 = [[UIViewController alloc] initWithNibName:name];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:vc1];
......
UIViewController *vc5 = [[UIViewController alloc] initWithNibName:name];
UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:vc5];
myTabBarController.viewControllers = [NSArray arrayWithObjects:nav1,...,nav5, nil];
[self.window addSubview:myTabBarController.view];
something like this...
p.s. sorry for bad formatting, hope you pick an idea.

Resources