I want to change the rootViewController after the authenticationViewController
-(IBAction)LoginButtonPushed:(id)sender {
if ([(VerifyId)isEqual:#"C"]){
CondidatsViewController *condidatsViewController = [[[CondidatsViewController alloc]initWithNibName:#"CondidatsViewController" bundle:nil]autorelease];
UINavigationController *navController = self.navigationController;
NSMutableArray *controllers = [[self.navigationController.viewControllers mutableCopy] autorelease];
[controllers removeLastObject];
navController.viewControllers = controllers;
[navController pushViewController:condidatsViewController animated: YES];
} else {
RecruteursViewController *recruteursViewController = [[[RecruteursViewController alloc]initWithNibName:#"RecruteursViewController" bundle:nil]autorelease];
UINavigationController *navController = self.navigationController;
NSMutableArray *controllers = [[self.navigationController.viewControllers mutableCopy] autorelease];
[controllers removeLastObject];
navController.viewControllers = controllers;
[navController pushViewController:recruteursViewController animated: YES];
}
}
this code is when i press in login button i want CondidatsViewController or RecruteursViewController will be the rootView
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
AcceuilViewController *viewController =[[[AcceuilViewController alloc]
initWithNibName:#"AcceuilViewController" bundle:nil]autorelease];
self.navController = [[[UINavigationController alloc]initWithRootViewController:viewController]
autorelease];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
return YES;
}
You can try UINavigationController's following method with a new array of desired view controllers something like
[self.navigationController setViewControllers:#[newRootViewControllerInstance, secondVCInstanceIfRequired, thirdVC-and-so-on....] animated:NO];
It will do the trick ;)
How about have a view controller one level above your nav and auth view controllers? This view controller can check for a valid session and push whatever view is appropriate on top of the stack. It seems ill advised to try to change your root view controller.
Edit: More details
You have a root view controller that doesn't have a view tied to it like most other view controllers might. You set this class as the root view controller in your app delegate.
App Delegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
RootViewController *rootViewController = [[RootViewController alloc] init];
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
In your root view controller you can determine if you have a valid session or not. Based on that, you can render the appropriate view.
RootViewController.m
#interface RootViewController ()
#end
#implementation RootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
BOOL isUserAuthenticated = [MyClasskToFigureOutIfUserIsAuthenticated isUserAuthenticated];
if(isUserAuthenticated == NO) {
AuthViewController *vcAuth = [[AuthViewController alloc] init];
[self addChildViewController:vcAuth];
[self.view addSubView:vcAuth.view];
} else {
//they are authenticated so push your other view controller.
}
}
#end
This is pretty rough, but it should point you in the right direction.
Related
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;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setWindow:[[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]];
// Override point for customization after application launch.
//self.window.backgroundColor = [UIColor whiteColor];
// Create First view
FirstViewController *firstViewCTL = [[FirstViewController alloc] init];
// Create UINavigationController
UINavigationController *navCTL = [[UINavigationController alloc] init];
[[navCTL navigationBar] setBarStyle:UIBarStyleBlack];
[navCTL pushViewController:firstViewCTL animated:NO];
[firstViewCTL release];
[[self window] addSubview:navCTL.view];
[navCTL release];
[[self window] makeKeyAndVisible];
return YES;
}
I understand that adding subview (addSubview:) will retain the view to be aded. But why I can't now release my navigation controller (navCTL) that own the views that has already been retained
-addSubview: retains the view, not the view controller.
You can use UIWindow's rootViewController property (iOS 4 and higher) to retain the view controller, it also saves you adding the view as a subview yourself.
...
[window setRootViewController:navCTL];
[navCTL release];
...
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];
}
in my viewcontroller,I have a button,when press the button,entry the navigationController,my code like:
-(IBAction)ShangHaiButtonPressed:(id)sender{
marketviewcontroller = [[MarketViewController alloc]initWithNibName:#"MarketViewController" bundle:nil];
NSLog(#"%#",self.navigationController);
[self.navigationController pushViewController:marketviewcontroller animated:YES];
[marketviewcontroller release];
}
but I can see the self.navigationController is null,how to solve this problem?thank you.
update:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_switchviewcontroller = [[SwitchViewController alloc]initWithNibName:#"SwitchViewController" bundle:nil];
[self.window addSubview:_switchviewcontroller.view];
[self.window makeKeyAndVisible];
return YES;
}
The navigationController property of a view controller will return a valid navigation controller object only if the view controller is in a navigation controller's navigation stack. A view controller can be added to a navigation stack in the following ways.
By making the view controller the rootViewController of a navigation controller using initWithRootViewController: method of UINavigationController
By pushing the view controller using pushViewController: method of UINavigationController.
Make sure your view controller is added to the navigation stack in any of the above ways.
EDIT: (After the didFinishLaunchingWithOptions: code added to the question):
Change the didFinishLaunchingWithOptions: method to this,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_switchviewcontroller = [[SwitchViewController alloc]initWithNibName:#"SwitchViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:_switchviewcontroller];
[self.window addSubview:navController.view];
[navController release];
[self.window makeKeyAndVisible];
return YES;
}
Swift 4 (version):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
switchviewcontroller = SwitchViewController(nibName: "SwitchViewController", bundle: nil)
let navController = UINavigationController(rootViewController: switchviewcontroller)
window.addSubview(navController.view)
window.makeKeyAndVisible()
return true
}
This code will yield the solution you're looking for:
-(IBAction)ShangHaiButtonPressed:(id)sender {
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self];
[self.view removeFromSuperview];
[appDelegate.window addSubview:nav.view]; // appDelegate is the delegate of your Application
marketViewController = [[MarketViewController alloc] initWithNibName:#"MarketViewController" bundle:nil];
[nav pushViewController:marketViewController animated:YES];
[marketViewController release];
}
In AppDelegate.m file make your first view RootView for Navigation :
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
myView *Obj=[[myView alloc]initWithNibName:#"myView" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:Obj];
nav.navigationBar.barStyle = UIBarStyleBlackOpaque;
[window addSubview:nav.view];
[self.window makeKeyAndVisible];
return YES;
}
In your myView.m file add below code to navigate to myNewView from myView :
-(void) registerMethod {
myNewView *obj = [[myView alloc] initWithNibName:#"myNewView" bundle:nil];
[self.navigationController pushViewController:obj animated:YES];
[obj release];
}