pushviewcontroller is not work in xcode6 - ios

The below code is work in ipad,but not in iphone.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UIViewController *controller = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MoreAppsViewController"];
UINavigationController *navgiate = [[UINavigationController alloc]initWithRootViewController:controller];
[self.navigationController pushViewController:navgiate animated:YES];
i cannot able to load MoreApsviewController by instatinating. View Controller is not pushing Please help me how to do it. Thanks in Advance

If your container is a navigation controller, you can simply intilize a VC and push it like this;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UIViewController *controller = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MoreAppsViewController"];
//No need to create a nav controller here
[self.navigationController pushViewController:controller animated:YES];
If you want to add another navigation controller you can present it or add as child VC, but you can't push another navigationVC.

These 2 steps should be checked.
If the View Controller you are trying to access is embedded in a Navigation controller you are correct in referencing the correct view, otherwise, change UINavigationController *navgiate to UIViewController * navigate (OR UITableViewController * navigate if it is a Table View).
Check your referencing of View Controllers. If you have the name (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MoreAppsViewController"]; I will assume that you have named your View Controller MoreAppsViewController. You need to name and reference the UINavigationController as it is the point of reference.
This is the result for View Controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UIViewController *controller = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MoreAppsViewController"];
[self.navigationController pushViewController:controller animated:YES];
This is the result for Navigation Controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
//You need to name your Navigation Controller "MoreAppsViewController" for this to work
UINavigationController *controller = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:#"MoreAppsViewController"];
[self.navigationController pushViewController:controller animated:YES];

Related

why use storyboard push a viewcontroller shows black screen

I created a SingleView application then use "Embed In Navigation Controller" to get a navigation control.
when push a controller, the viewcontroller's backgroundColor is black.
i know use this code :
UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];//UIStoryboard(name: "Main", bundle:nil)
UIViewController *roomController = [storyBoard instantiateViewControllerWithIdentifier:#"controlerID"];
but what's the reason ?
and if i don't want to use the storyboard completely ,only use this code
UIViewController *roomController = [[UIViewController alloc]init];
what can i do? is only set the viewcontroller's backgroundColor?
You need to use the instantiateViewControllerWithIdentifier: method of your UIStoryboard instance and then you can push the controller.
UIStoryboard *storyboard =[UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *roomController = [storyboard instantiateViewControllerWithIdentifier:#"controlerID"];
[self.navigationController pushViewController: roomController animated:YES];
// if You make controller with out storyboard Than change You view color
UIViewController *roomController = [[UIViewController alloc]init];
[self.navigationController pushViewController: roomController animated:YES];
- (void)viewDidLoad
{
self.view.backgroundColor =[UIColor whiteColor];
}

Navigation and Tab Bar Controller not showing up

I'm trying to use instantiateViewControllerWithIdentifier in the app delegate to show a view controller but for some reason the navigation bar and tab bar isn't show up. I'm not sure what I'm doing wrong - Thanks
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *view = (UINavigationController *)[sb instantiateViewControllerWithIdentifier:#"ShopViewController"];
self.window.rootViewController = view;
Is ShopViewController a UINavigation controller or is it just a view controller. It sounds like you are declaring the view controller as uinavigationcontroller.
Instead you should either drop a navigation controller on the storyboard and then give that an identifier OR you could just create a navigation controller in the app delegate.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ShopViewController *showViewController = (ShopViewController *)[sb instantiateViewControllerWithIdentifier:#"ShopViewController"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:shopViewController];
self.window.rootViewController = nav;

Change View on successful registration using storyboard

I have three UIViewControllers ViewController, HomeViewController and RegisterViewController using storyboard. Their Storyboard ID is Login, Home and Registration respectfully. There is a button on RegisterViewController view named 'Register'. After successful registration i want to go to home page i.e., HomeViewController. How to do it. Please help I'm new to iOS.
thanks
- (IBAction)yourRegisterButtonAction:(id)sender
{
[self performSegueWithIdentifier:#"YOUR_SEUGE_NAME" sender:nil];
}
Try This
- (IBAction)buttonRegister:(id)sender {
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
// replace Main with your storyboard name
HomeViewController *homeVC = [storyboard instantiateViewControllerWithIdentifier:#"Home"];
[self.view addSubview:homeVC.view];
}
Implement a navigation controller and do this.
-(IBAction)registerButtonTapped:(id)sender{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
HomeViewController *viewController =(HomeViewController*) [storyboard instantiateViewControllerWithIdentifier:#"Home"];
[self.navigationController pushViewController:viewController animated:YES];
}

How to set properties of a root view controller before being presented?

I have a certain UINavigationController in storyboard I present modally from another view controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *myNavController = [storyboard instantiateViewControllerWithIdentifier:#"myNavController"];
[self presentViewController:myNavController animated:YES completion:nil];
This navigation controller has set another UIViewController as its root view controller in storyboard. I'd like to set some properties for this root view controller before it is shown, but I tried this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *myNavController = [storyboard instantiateViewControllerWithIdentifier:#"myNavController"];
[self presentViewController:myNavController animated:YES completion:nil];
MyRootViewController *myRootViewController = [storyboard instantiateViewControllerWithIdentifier:#"myRootViewController"];
[myRootViewController setSelectedItem:selectedItem];
[myRootViewController setDelegate:self];
But the root view controller doesn't seem to be loaded yet when I try to set its properties...
How could I do this? Thanks
If you had debugged, you will find the myRootViewController made by MyRootViewController *myRootViewController = [storyboard instantiateViewControllerWithIdentifier:#"myRootViewController"];
isn't the myNavController's real rootViewController.
This method -instantiateViewControllerWithIdentifier: just give you a whole new UIViewController instance.
So, If you want get the real rootViewController, just replace it with following:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *myNavController = [storyboard instantiateViewControllerWithIdentifier:#"myNavController"];
MyRootViewController *myRootViewController = myNavController.viewControllers[0];
[myRootViewController setSelectedItem:selectedItem];
[myRootViewController setDelegate:self];
[self presentViewController:myNavController animated:YES completion:nil];

How to pop all view and go back to login view when user tap logout button

I use a navigation controller for login view.From there i push to a tabbar controller using this code
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UITabBarController *obj=[storyboard instantiateViewControllerWithIdentifier:#"tab"];
self.navigationController.navigationBarHidden=YES;
[self.navigationController pushViewController:obj animated:YES];
How can navigate back to login view when user tap logout from profile screen (one of the tabbar controller view). I want a navigation bar in the login view. Can anyone please help.
I am using Storyboard.
Try this:
[[self.parentViewController navigationController] popToRootViewControllerAnimated:YES];
Since you are pushing a TabBarController, you will need to pop using the TabBarController. self.parentViewController should give you the TabBarController.
You could use the following :-
//This would pop to the given VC
[self.navigationController popToViewController:loginViewController animated:YES];
//This would pop to the root VC
[self.navigationController popToRootViewControllerAnimated:YES]
Update:-
- (IBAction)loginBtn{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
//U provided UITabBarController so replace at with your tabBar controller class like below ,TabViewController class is created.
TabViewController *obj=[storyboard instantiateViewControllerWithIdentifier:#"tabBar"];
self.navigationController.navigationBarHidden=YES; //navigation bar hidden so won't appear when back again to login if want to see then make it true in viewWillAppear/viewWillLoad.
[self.navigationController pushViewController:obj animated:YES];
}
Also for coming back from tabBar to login VC follow below,
- (IBAction)popbtn:(id)sender{
[self.navigationController popToRootViewControllerAnimated:YES];
}
Also assgin the class to tabBar controller in storyboard along with storyboard id as below:-
You can use just one line of code.
[self.navigationController pushViewController:viewController animated:YES];
id rootController = [[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] nextResponder];
if([rootController isKindOfClass:[LoginViewController class]]){
//do something
[self.navigationController popToRootViewControllerAnimated:YES];
}
else{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"login"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
}

Resources