I have a problem with switching from one viewcontroller to another.
Here is what I have :
In my "AppDelegate_iPad.m" :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect frame = [UIScreen mainScreen].bounds;
window = [[UIWindow alloc] initWithFrame:frame];
window.backgroundColor = [UIColor blackColor];
LoginViewController_iPad *loginView = [[LoginViewController_iPad alloc] initWithNibName:#"LoginViewController_iPad" bundle:nil];
[window addSubview:loginView.view];
[self.window makeKeyAndVisible];
return YES;
}
That works. Now I have a login button on that screen and when pressed it calles this :
- (IBAction)doPressLoginButton
{
DebugLog(#"doPressLoginButton");
MenuViewController_iPad *menuView = [[MenuViewController_iPad alloc] initWithNibName:#"MenuViewController_iPad" bundle:nil];
[self.navigationController pushViewController:menuView animated:YES];
[menuView release];
}
The problem is that nothing happends. I assume I am missing the actual navigationconroller ?!
Hope that anyone can help.
Thank you
You should create a UINavigationController instance variable in your app delegate. Then, make sure you synthesize it and release it in your dealloc method.
Your implementation of application:didFinishLaunchingWithOptions: could look like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// window is usually an IBOutlet
CGRect frame = [UIScreen mainScreen].bounds;
window = [[UIWindow alloc] initWithFrame:frame];
window.backgroundColor = [UIColor blackColor];
LoginViewController_iPad *loginView = [[LoginViewController_iPad alloc] initWithNibName:#"LoginViewController_iPad" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:loginView];
[loginView release];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Now you'll be able to use self.navigationController.
I assume I am missing the actual navigationconroller ?!
you are right. :D
self.navigationController returns nil if you don't set up an NavigationController.
And any messages send to nil object will be ignored.
If you only need switch from one to another.
using
[self presentModalViewController:modalViewController animated:YES];
instead.
Related
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!
I have created a new project "Empty Application" template in Xcode 4.3, it is having only two classes AppDelegate.h & .m
I checked with ARC to use automatic reference count while creating the app.
I added two new files "RootViewController" & "NewProjectViewControllers".
I implemented code to set navigation controller as follows in AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
rootViewController = [[MainViewController alloc] init];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[self.window addSubview:navigation.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
and in hte home view (Root view controller) implemented as follows
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Projects";
UINavigationBar *navigationBar = [self.navigationController navigationBar];
[navigationBar setTintColor: [UIColor colorWithRed:10/255.0f green:21/255.0f blue:51/255.0f alpha:1.0f]];
//To set the customised bar item
UIButton *rightBarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[rightBarBtn setBackgroundImage:[UIImage imageNamed:#"plus_new.png"] forState:UIControlStateNormal];
rightBarBtn.frame=CGRectMake(0.0, 100.0, 30.0, 30.0);
[rightBarBtn addTarget:self action:#selector(addProject) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarBtn];
self.navigationItem.rightBarButtonItem = rightBarItem;
// Do any additional setup after loading the view from its nib.
}
- (void) addProject
{
NewProjViewController *editProject = [[NewProjViewController alloc] init];
[self.navigationController pushViewController:editProject animated:YES];
NSLog(#"xxxxxxxxxxxxxxx");
}
But since i used ARC the navigation may dealoc immediately and it doesn't work, All the actions in method works except push to the next view
if i do same thing with out ARC it works fine
How to resolve this issue..? Thanks in advance
In Your appdelegate appdidfinishlaunching method, you have not set
self.window.rootviewcontroller to navigationController. In fact you did not set any rootViewController to Window. Thats why it is not being shown. Please set it before you start to see your ViewController on the screen.
The UIWindow rootViewController property is new with iOS4.
The older technique was to use addSubview.
The new, recommended technique is to set rootViewController.
Try this:
NewProjViewController *editProject = [[NewProjViewController alloc]initWithNibName:#"NewProjViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:editProject animated:YES];
[editProject release];
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 need to implement the following and I wanted to know the correct way to do it.
when the iPhone application launches, I need to show a logo image for 2 seconds followed by showing a login screen that allows the person to login or create an account. Once the person logs in, i need to show a tabbarcontroller menu options.
This is how I'm currently doing it:
In the AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
LoginViewController *viewController0 = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *aNavigationController0 = [[UINavigationController alloc] initWithRootViewController:viewController0];
self.window.rootViewController = aNavigationController0;
// I also implement an iVar of the UITabBarController here...
// ....
}
The #implementation:
#implementation LoginViewController
- (IBAction)createNewAccountButtonClicked:(id)sender {
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
delegate.window.rootViewController = delegate.tabBarController;
}
So, my questions are:
Is this the correct way to show the tabbar for my purpose?
In this scheme of things, I cannot show the logo animated. Any pointers on how to do this?
The code below assumes you're using ARC, if you're not then you'll need to do your MRC.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
self.window.rootViewController = self.tabBarController;
LoginViewController *loginViewController= [[LoginViewController alloc] initWithNibName:nil bundle:nil];
loginViewController.delegate = self;
UINavigationController *loginNavCont = [[UINavigationController alloc] initWithRootViewController:loginViewController];
[self.tabBarController presentModalViewController:loginNavCont animated:NO];
UIImageView *splashScreen = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default"]];
[self.window addSubview:splashScreen];
[UIView animateWithDuration:0.5
delay:2.0
options:0
animations:^{
splashScreen.alpha = 0.0;
}
completion:^(BOOL finished) {
[splashScreen removeFromSuperview];
}];
[self.window makeKeyAndVisible];
return YES;
}
- (void)loginViewControllerShouldBeDismissed:(UIViewController *)viewController
{
[self.tabBarController dismissModalViewControllerAnimated:YES];
}
I am new bee in iPhone. I have implmented using some tutorials, a Splash Screen before loading the UIViewController. Now i want to implement a NavigationController in my application and want to display a Splash Screen before it. Since I am new in Iphone so i did not get any tutotrials or guides to make a Splash Screen before loading a Root Navigation Controller.
I have seen many methods in which they over write the Default.png file and so on. I dont want to implement that one. I want a sperate UIView to have my custom Images and text in it and display that UI View as a Splash Screen
can anybody Guide me please.
Thanks in advance
Here you go buddy. Have fun and happy coding....
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Add a splash screen
UIImageView *imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"splash.png"]];
imgv.userInteractionEnabled = YES;
[navigationController.view addSubview:imgv];
[imgv release];
[self performSelector:#selector(removeSplash:) withObject:imgv afterDelay:3.0];
[window addSubview:navigationController.view];
return YES;
}
- (void)removeSplash:(UIImageView *)imageView {
[imageView removeFromSuperview];
}
Use "self.window" to display the splash image first. If u simply write "window", the image will not be displayed and animated in the first view, since the image can't be directly linked to the window in that case. Write the following code in appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
UIImageView *imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"themes.png"]];
imgv.userInteractionEnabled = YES;
[self.navigationController.view addSubview:imgv];
//[imgv release]; If you don't use ARC, uncomment this.
[self performSelector:#selector(removeSplash:) withObject:imgv afterDelay:3.0];
[self.window addSubview:self.navigationController.view];
return YES;
}
- (void)removeSplash:(UIImageView *)imageView
{
[imageView removeFromSuperview];
}