self.navigationController is null - iphone

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];
}

Related

how to change the rootviewcontroller

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.

change RootviewController to Navigation controller

I am new to iPhone,
I want to change my Rootviewcontroller to my new class and make it to navigation controller.
Here is my code snippet,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:detailViewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
I am getting SIGABRT says 'adding a root view controller <NewClass: 0x6a8dd50> as a child of view controller:
Whenever u want to set:
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:detailViewController];
self.window.rootViewController =nil;
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
EDIT : Directly use AppDelegate instance to set rootViewController for UIWindow as i have shown above.
Instead of:
[self.window addSubview:navigationController.view];
make navigationController the rootViewController of window:
self.window.rootViewController = navigationController;
Also, is detailViewController of type UINavigationController? You cannot set UINavigationController as root to another UINavigationController object.
Just add this line,
RootViewController *defaultViewController=[[RootViewController alloc]initWithNibName:#"NAME_OF_XIB" bundle:nil];
before UINavigationController's initialization,
RootViewController *defaultViewController=[[RootViewController alloc]initWithNibName:#"NAME_OF_XIB" bundle:nil];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:detailViewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
Change your RootViewController to NavigationController..
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:self.detailViewController];

how to show a tab bar controller after the login screen is launched?

This is a view based application.
in delegate.m file I have done like this to launch login screen initially:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
[window addSubview:viewController.view];
[window makeKeyAndVisible];
LoginView *loginView=[[LoginView alloc]initWithNibName:#"LoginView" bundle:nil];
[window addSubview:loginView.view];
}
By adding the above code I have launched login screen sucessfully, but at the bottom of my login screen I can see a space left out.
How can the tab bar controller get launched after sucessful login?
i have creatd a method called login in my LoginView.m file:
-(void)login
{
if(login)
{
TabBarController *tabBarController = [[TabBarController alloc] initWithNibName:#"TabBarController" bundle:nil];
[self.view addSubView: aTabBarController.view];
}
[aTabBarController release];
Please help me out of this with the appropriate code.
you have to create on method in appDelegate like.. and In appDelegate.h you have to create an object like this
UITabBarController *Obj_tabbar;
and then in .m file,
-(void) switchToTabbarController
{
Obj_tabbar.delegate = self;
Obj_tabbar.selectedIndex = 0;
Tracking_HomeVC *obj = [[Tracking_HomeVC alloc]init];
[self tabBarController:Obj_tabbar didSelectViewController:obj];
[self.window addSubview:Obj_tabbar.view];
}
// At this point Tracking_HomeVC is the first view controller of the TabbarController. and it will be added on window.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if([tabBarController selectedIndex] == 0)
{
//Write your code here to do with the first view controller object.
}
}
and then call it from your LoginView like..
-(void)LoginPressed
{
AppAppDelegate *delegate =(AppAppDelegate *) [[UIApplication sharedApplication] delegate];
[delegate switchToTabbarController];
}
Your login view (or it's controller if you have one which it looks like you don't) should tell the appDelegate to swap the RootViewController to be a taBarController. You do NOT want the loginview to be trying to add a tabBar as a child of itself.
One way of doing it is creating a tabbarcontroller like normal in your appdelegate and set it as rootviewcontroller:
TOTabBarController *tabBarController = [[TOTabBarController alloc] init];
UIViewController *vc1 = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UIViewController *vc2 = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UIViewController *vc3 = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *vc2_nc = [[UINavigationController alloc] initWithRootViewController:vc2];
UINavigationController *vc3_nc = [[UINavigationController alloc] initWithRootViewController:vc3];
NSArray *viewControllers = [NSArray arrayWithObjects:vc1, vc2_nc, vc3_nc, nil];
[tabBarController setViewControllers:viewControllers];
//set tabbarcontroller as rootviewcontroller
[[self window] setRootViewController:tabBarController];
Then display the login screen modally (without animation) if the user is not logged in:
if (not logged in) {
UIViewController *lvc_nc = [[UIViewController alloc] init];
[[[self window] rootViewController] presentModalViewController:lvc_nc animated:NO];
}
Hope that helps!

ModalView doesnt show up with tabbarcontroller

I am building an app that has a login screen that leads to a tabbar. I followed this example on how to push a modal view once you launch the app (as the "sign in" page) and then dismiss it.
Example --> Show / Hide tab bar
From some reason, it's not really working - when I launch the app, I see the tabbar view with the two view controllers. no sign in page.
here is my code:
AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstTab alloc] initWithNibName:#"FirstTab" bundle:NSBundle.mainBundle];
UIViewController *viewController2 = [[SecondTab alloc] initWithNibName:#"SecondTab" bundle:NSBundle.mainBundle];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, secondNavController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
My first tab (which is where I understand all this modal view business needs to happen)
.h
#interface FirstTab : UIViewController
#end
.m
//UPDATED CODE PER COMMENT BELOW
- (void)viewDidLoad
{
[super viewDidLoad];
SignIn *loginview = [[SignIn alloc] initWithNibName:#"SignIn" bundle:nil];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController: loginview];
self.hidesBottomBarWhenPushed = YES;
[self presentModalViewController:controller animated:YES];
}
And of course, I dismiss the modal view in the SignIn view controller, though I never get there as I mentioned.
What am I doing wrong here? Thanks for the help!!
You could use :
[[self tabBarController] presentModalViewController:controller animated:YES];
since first viewController1 is your first tab, and self.navigationController might be nil.
In your custom view controller subclass called SignIn implement initWithNibName:bundle: instead if init.
-(id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// init here
}
}
Now when init/alloc it call either :
SignIn *loginview = [[SignIn alloc] initWithNibName:#"SignIn" bundle:nil];
if your interface is in a NIB file, or :
SignIn *loginview = [[SignIn alloc] initWithNibName:nil bundle:nil];
if there no NIB.
Also why putting it as a root view controller of any navigation controller ? unless you need to go deeper in some model data presentation, just present it directly :
// why ?
//UINavigationController *controller = [[UINavigationController alloc]
// initWithRootViewController: loginview];
//self.hidesBottomBarWhenPushed = YES;
//[self presentModalViewController:controller animated:YES];
[self presentModalViewController:loginView animated:YES];
You have to include this in your code,
yourModelController =[[Yourmodelcontroller alloc]init];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController: yourModelController];
self.hidesBottomBarWhenPushed = YES;
[[self navigationController] presentModalViewController:controller animated:YES];
[controller release];
hope this will help you out.
I've encountered issues where modals don't like showing from viewDidLoad. Try adding your code to your viewWillAppear and it should show.

iphone - cant push modal views after adding subview to window?

Im my application I have the following code, the first bit runs when my application launches, and it presents a login screen in a modal view. With my rootController added to the window before that.
The Modal view will allow a user to log on, view terms and conditions and then if they accept the loadMainApp function is called.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
rootController.delegate = self;
[window addSubview:rootController.view];
LoginViewController *_loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginView" bundle:[NSBundle mainBundle]];
self.loginViewController = _loginViewController;
[_loginViewController release];
UINavigationController *navi = [[[UINavigationController alloc]initWithRootViewController:loginViewController]autorelease];
navi.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[loginViewController release];
[rootController presentModalViewController:navi animated:YES];
[self.window makeKeyAndVisible];
return YES;
}
This next bit is called when the user accepts terms and conditions:
-(void)loadMainApp:(UIView *)fromView{
[fromView removeFromSuperview];
[window addSubview:rootController.view];
rootController.selectedIndex = 2;
rootController.delegate = self;
}
From here I want to be able to use the people picker which is a modal view and I attempt it with this code:
ABPeoplePickerNavigationController *picker =
[[ABPeoplePickerNavigationController alloc]init];
picker.peoplePickerDelegate = self;
[rootController presentModalViewController:picker animated:YES];
[picker release];
But nothing happens, when I try to present a modal view from my rootController nothing displays.
This appears to occur after this code in applicationDidFinishLaunching is run:
LoginViewController *_loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginView" bundle:[NSBundle mainBundle]];
self.loginViewController = _loginViewController;
[_loginViewController release];
UINavigationController *navi = [[[UINavigationController alloc]initWithRootViewController:loginViewController]autorelease];
navi.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[loginViewController release];
[rootController presentModalViewController:navi animated:YES];
[self.window makeKeyAndVisible];
return YES;
If I dont display the login screen at all and instead just add my rootController
rootController.delegate = self;
[window addSubview:rootController.view];
in the applicationDidFinishLaunching function then everything works fine when I go to display the people picker.
Is there something fundamental that I am breaking here or can anyone spot what I may be doing wrong?
Are you dismissing the first modal view controller correctly, via thedismissModalViewControllerAnimated: method? If you're just removing the modal view controller's view from its superview, then probably a lot of paperwork is being left undone by UIKit.
What you should really do is something like this:
Add rootViewController to navigationController. Then add navigationController to the window.
In rootViewController's viewDidLoad method, set loginViewController's delegate to rootViewController and display the LoginViewController's instance as a modal.
When you are done with terms acceptance, do [delegate loginViewControllerFinished];.
Now in rootViewController, handle the loginViewControllerFinished method. This should contain a call to [self dismissModalViewControllerAnimated:YES]; to close the login modal view. Then you should launch ABPeoplePickerNavigationController as a modal view.
In AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Make sure rootViewController is initiated by this point.
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
[window addSubview:rootController.view];
[self.window makeKeyAndVisible];
return YES;
}
In RootViewController:
- (void)viewDidLoad {
[super viewDidLoad];
LoginViewController *loginViewController = [[[LoginViewController alloc] initWithNibName:#"LoginView" bundle:nil] autorelease];
loginViewController.delegate = self;
loginViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:loginViewController animated:YES];
}
- (void)loginViewControllerFinished {
[self dismissModalViewControllerAnimated:YES];
// Here we are closing one modal. And showing another after that.
ABPeoplePickerNavigationController *peoplePicker = [[[ABPeoplePickerNavigationController alloc] init] autorelease];
peoplePicker.peoplePickerDelegate = self;
[self presentModalViewController:peoplePicker animated:YES];
}
In LoginViewController:
// Make sure LoginViewController has delegate property in header.
// #property (nonatomic, assign) id delegate;
// And put this in implementation (.m) file. #synthesize delegate.
// Don't put release for delegate, since it's not retained. It's only 'assigned'.
- (void)done {
// Call this when you want to close loginViewController.
[delegate loginViewControllerFinished];
}
Hope this helps.