I want to know that how can we setup a UIViewController in a UINavigationController without using UITableView to its Root View Controller. Any help regarding this? Thanks in advance.
You can do that as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
UIViewController *myViewController = [[UIViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
self.window.rootViewController = navigationController;
// ...
}
You can replace the class of myViewController and/or initialize it using a nib if you want.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:[[OptionViewController alloc]initWithNibName:#"OptionViewController" bundle:nil]];
self.window.rootViewController=nav;
self.window.backgroundColor = [UIColor grayColor];
[self.window makeKeyAndVisible];
return YES;
}
It's working for me:
set your tabviewcontroller on window, then it will work properly.
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UITabBarController *tabVC = [self.storyboard instantiateViewControllerWithIdentifier:#"MyMainView"];
[window setRootViewController:tabVC];
UINavigationController pushViewController:animated:
Related
There is a 100% of memory leak shows by instrument at self.window.rootViewController= navigationController;. Application uses ARC.
UINavigationController,UIViewController and window are all properties have attribute strong.
How can i fix this leak.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
txnObserver = [[InAppPurchaseObserver alloc] init];
txnObserver.delegate = self.viewController;
[[SKPaymentQueue defaultQueue] addTransactionObserver:txnObserver];
navigationController = [[UINavigationController alloc]initWithRootViewController:viewController];
**self.window.rootViewController= navigationController;**
[self.window makeKeyAndVisible];
return YES;
}
Same code did not show any leak in iOS 6 but it shows leak in iOS 7.
Updated question on 2013/10/10 with details.
You can change your didFinishLaunchingWithOptions by this following code without #property of viewController :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
with #property (nonatomic, strong) ViewController *viewController; :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
This is my AppDelegate, i set a title "Pizza", but when i build and play the application, the text don't will appear.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UITabBarController *tabBarController = [[UITabBarController alloc]init];
ListaPizzeViewController *listPizzeVC = [[ListaPizzeViewController alloc]init];
UINavigationController *navigationControllerPizza = [[UINavigationController alloc]initWithRootViewController:listPizzeVC];
[navigationControllerPizza setTitle:#"Pizza"];
navigationControllerPizza.tabBarItem.image = [UIImage imageNamed:#"Default.png"];
ListIngredientsViewController *listIngredientVC = [[ListIngredientsViewController alloc]init];
UINavigationController *navigationControllerIngradient = [[UINavigationController alloc]initWithRootViewController:listIngredientVC];
[navigationControllerIngradient setTitle:#"Ingredient"];
navigationControllerIngradient.tabBarItem.image = [UIImage imageNamed:#"Default.png"];
[tabBarController setViewControllers:#[navigationControllerPizza, navigationControllerIngradient]];
[self.window setRootViewController:tabBarController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Why?
According to Your Question you can set title of ListaPizzeViewController using object of ListaPizzeViewController like Bellow:-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UITabBarController *tabBarController = [[UITabBarController alloc]init];
ListaPizzeViewController *listPizzeVC = [[ListaPizzeViewController alloc]init];
UINavigationController *navigationControllerPizza = [[UINavigationController alloc]initWithRootViewController:listPizzeVC];
[listPizzeVC setTitle:#"Pizza"]; // Here it is you can set Title of Object of Viewcontroller so set like thin not [navigationControllerPizza setTitle:#"Pizza"];
navigationControllerPizza.tabBarItem.image = [UIImage imageNamed:#"Default.png"];
ListIngredientsViewController *listIngredientVC = [[ListIngredientsViewController alloc]init];
UINavigationController *navigationControllerIngradient = [[UINavigationController alloc]initWithRootViewController:listIngredientVC];
[navigationControllerIngradient setTitle:#"Ingredient"];
navigationControllerIngradient.tabBarItem.image = [UIImage imageNamed:#"Default.png"];
[tabBarController setViewControllers:#[navigationControllerPizza, navigationControllerIngradient]];
[self.window setRootViewController:tabBarController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
go into your ListaPizzeViewController, inside the viewDidLoad write
self.title = #"Pizze";
self.navigationItem.title=#"pizza";
simply call this.
Try to set title in viewWillAppear method.
of "listPizzeVC" as
self.title=#"Pizza";
IT is title is ViewController property so use view controller object and title set I write the code under.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UITabBarController *tabBarController = [[UITabBarController alloc]init];
ListaPizzeViewController *listPizzeVC = [[ListaPizzeViewController alloc]init];
UINavigationController *navigationControllerPizza = [[UINavigationController alloc]initWithRootViewController:listPizzeVC];
//[navigationControllerPizza setTitle:#"Pizza"];
listPizzeVC.title=#"Pizza";
navigationControllerPizza.tabBarItem.image = [UIImage imageNamed:#"Default.png"];
ListIngredientsViewController *listIngredientVC = [[ListIngredientsViewController alloc]init];
UINavigationController *navigationControllerIngradient = [[UINavigationController alloc]initWithRootViewController:listIngredientVC];
//[navigationControllerIngradient setTitle:#"Ingredient"];
listIngredientVC.title=#"Ingredient";
navigationControllerIngradient.tabBarItem.image = [UIImage imageNamed:#"Default.png"];
[tabBarController setViewControllers:#[navigationControllerPizza, navigationControllerIngradient]];
[self.window setRootViewController:tabBarController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
I hope it is useful too.
Your code should not be in the appDelegate but in a viewcontroller class that inherits from UIViewController and attach this class to your xib or storyboard screen. One viewcontroller per screen. Add your code to the viewDidLoad or create your own methods. When this doesn't work you might wanna try and post your question again. ;).
I recently started iOS development, it helped me to take little steps and create small project that aimed for learning one thing at a time this way you will learn really fast and efficient without too much frustration. Good luck!
In my project the navigation bar is being coming only in rootview(homeview) for the first only,i want to enable navigation bar in all views?Here my code?What change should i do for that
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.window.rootViewController =navigationController;
[self.window addSubview:[navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
Please help me to code?
See this link
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
navigationController =[[UINavigationController alloc] init];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[navigationController pushViewController:viewController2 animated:NO];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
And when you navigate to the another view from viewController2 do pushViewController not others like presentView
[self.navigationController pushViewController:anotherViewController animated:YES];
Put this code in a delegate class.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Set the navigation controller as the window's root view controller and display.
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
If you want to enable navigation in all the views then you will have to declare the navigation in AppDelegate.m wherein it will be viewable in all the views. I dont have my mac rite now but its the best advice I can provide for now :)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.window.rootViewController =navigationController;
[self.window addSubview:[navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
Change the uiviewcontroller to SecondViewController
try this way may be helped you
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// [NSThread sleepForTimeInterval:0.1]; // simulate waiting for server response
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
// Initialise the navigation controller with the first view controller as its root view controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
// This is where we hide the navigation bar! :)
[navigationController setNavigationBarHidden:NO];
// Navigation controller has copy of view controller, so release our copy
//[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[self.viewController release];
// Add the navigation controller as a subview of our window
[_window addSubview:[navigationController view]];
[_window makeKeyAndVisible];
return YES;
}
This may be a stupid question but I programmatically added a UINavigationController to my app. If possible, I wanted to just add it to the top of all my windows except for the very first .xib. Maybe even just hide it on my first .xib. Is it possible to even do that? I think of my first .xib file that opens up to the rest of my app like a cover page and I rather that blue bar not show up at the top of that. I wish I could show you pictures but don't have enough reps yet. Thanks!
Below is the code I believe helps me to provide each page of app with the back bar:
#import "McCormick_TaylorViewController.h"
#implementation McCormick_TaylorAppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]
autorelease];
// Override point for customization after application launch.
self.viewController = [[[McCormick_TaylorViewController alloc]
initWithNibName:#"McCormick_TaylorViewController" bundle:nil] autorelease];
UINavigationController * navController = [[UINavigationController alloc]
initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
in your McCormick_TaylorViewController's viewWillApper: method
just use bellow code...
[self.navigationController setNavigationBarHidden:NO animated:YES];
and in other view controller in navigationbar ot display then in another viewController's viewWillAppear just use bellow code..
[self.navigationController setNavigationBarHidden:NO animated:NO];
Use this method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]
autorelease];
// Override point for customization after application launch.
self.viewController = [[[McCormick_TaylorViewController alloc]
initWithNibName:#"McCormick_TaylorViewController" bundle:nil] autorelease];
UINavigationController * navController = [[UINavigationController alloc]
initWithRootViewController:self.viewController];
[navController.navigationBar setHiden:YES]; // hides navigation bar
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
I m trying to change the color of navigation bar of UINavigationController by using
self.navigationController.navigationBar.tintColor = [UIColor brownColor];
in the below given code but its not working.
#import "uitextviewAppDelegate.h"
#import "uitextviewViewController.h"
#implementation uitextviewAppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
#synthesize navigationController = _navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[uitextviewViewController alloc] init];
UINavigationController *navigationController=[[UINavigationController alloc] init];
self.navigationController.navigationBar.tintColor = [UIColor brownColor];
[navigationController pushViewController:_viewController animated:YES];
[_window addSubview:navigationController.view];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Can anyone tell why it is not changing the color of the navigation bar of the UINavigation Controller.
Thanks in advance.
Looking at these two lines:
UINavigationController *navigationController=[[UINavigationController alloc] init];
self.navigationController.navigationBar.tintColor = [UIColor brownColor];
What connects the first navigationController with self.navigationController?
Nothing, as far as I can tell.
You need to assign self.navigationController to navigationController.
Something like:
self.navigationController = navigationController;