iOS - Push viewController from code and storyboard - ios

I have this code
PlaceViewController *newView = [self.storyboard instantiateViewControllerWithIdentifier:#"PlaceView"];
[self presentViewController:newView animated:YES completion:nil];
And I can change view, but I would push this view for when I return at this page, the state persists.
I try to put this code:
[self.navigationController pushViewController:newView animated:YES];
but doesn't do anything.
Thanks

Objective-C:
PlaceViewController *newView = [self.storyboard instantiateViewControllerWithIdentifier:#"storyBoardIdentifier"];
[self.navigationController pushViewController:newView animated:YES];
Please do notice below points,
your storyboard identifier is correct.
The root view have navigation Controller.
Swift:
let newView = self.storyboard?.instantiateViewController(withIdentifier: "storyBoardIdentifier") as! PlaceViewController
self.navigationController?.pushViewController(newView, animated: true)

For Storyboards, you should use performSegueWithIdentifier like so:
[self performSegueWithIdentifier:#"identifier goes here" sender:self];

Use:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"STORYBOARD_NAME" bundle:nil];
PlaceViewController *newView = [storyboard instantiateViewControllerWithIdentifier:#"PlaceView"];
[self presentViewController:newView animated:YES completion:nil];
Or:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"STORYBOARD_NAME" bundle:nil];
PlaceViewController *newView = [storyboard instantiateViewControllerWithIdentifier:#"PlaceView"];
[self.navigationController pushViewController:newView animated:YES];

Swift 3.x
let viewController = storyboard?.instantiateViewController(withIdentifier: "storyboardIdentifier") as! UIViewController
navigationController?.pushViewController(viewController, animated: true)

By default, Xcode creates a standard view controller. we first change the view controller to navigation controller. Select the Simply select “Editor” in the menu and select “Embed in”, followed by “Navigation Controller”
Step 1. Select Main story board
Step 2.Click on "Editor" on top of your Xcode application.
Step 3. Click on "Embed In"
Xcode automatically embeds the View Controller with Navigation Controller.

Related

Click new view controller on button click In IOS

I have a button action in which i'm trying to open view controller on condition bases, but the view controller are not opening. I have use story board identifier method to open the new view controller, i'm confused why it isn't opening. My condition is not working fine, i got null but when i got login and comes back to home screen again it again shows null.
My code is,
- (IBAction)History:(id)sender {
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:#"pin"];
if ([savedValue length]==0) {
NSLog(#"INNNNN");
UIStoryboard *story=[UIStoryboard storyboardWithName:#"Main" bundle:nil];
RegisterViewController *vc = [story instantiateViewControllerWithIdentifier: #"Register"];
[self.navigationController pushViewController:vc animated:YES];
}else{
UIStoryboard *story=[UIStoryboard storyboardWithName:#"Main" bundle:nil];
DetailsViewController *vc = [story instantiateViewControllerWithIdentifier: #"detail"];
[self.navigationController pushViewController:vc animated:YES];
}
}
The story board looks like this,
if your All viewcontrollers does not embed in UINavigationcontroller (Root) then use presentViewController for navigation.
- (IBAction)History:(id)sender {
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:#"pin"];
UIStoryboard *story=[UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *topViewVC;
if ([savedValue length]==0) {
RegisterViewController *vc = [story instantiateViewControllerWithIdentifier: #"Register"];
topViewVC = vc;
}else{
DetailsViewController *vc = [story instantiateViewControllerWithIdentifier: #"detail"];
topViewVC = vc;
}
[self presentViewController:topViewVC animated:true completion:nil];
}
option 2
if you want the pushViewController may be future purpose then dont do any changes in your code , just add UINavigationcontroller as root. Select your initial VC in storyboard then xcode menu --> Editor - -> Embed In --> Navigation Controller
It seems like you missed to embed your base view controller to navigation controller. Select your initial VC in storyboard. Go to Editor -> Embed In -> Navigation Controller.

How to open new view controller after click button?

I added one ViewController to my project and created one class.
After I bind this class to my ViewController.
In another controller I have method:
- (IBAction)login:(id)sender {
// How here I can do redirect to controllerViewController
}
There are two ways to push view controllers in the application.
1) by Segue
2) By View controller identifier
1) By Segue :
[self performSegueWithIdentifier:"SegueIdentifier" sender:self];
2) By View controller identifier :
Yourclassname *gotoYourClass = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerIdentifier"];
[self.navigationController pushViewController:gotoYourClass animated:YES];
- (IBAction)login:(id)sender {
ViewController *vc = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil]; }
In the storyboard give your view controller an identifier (under the Attributes Inspector) then use the following code, to bring that view forward.
IF YOU WANT TO USE PUSH THEN USE THIS CODE
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self.navigationController pushViewController:vc animeted:YES];
IF YOU WANT TO PRESENT THEN USE THIS CODE
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self presentModalViewController:vc animated:YES];
Note: In UIViewController please enter your view controller name which you want to push into another view.
If you want to comeback to the current ViewController later then use Navigation ViewController or else just use the presentedViewController of self(your current viewController)- no going back business, like they have previously illustrated.
For a simple block of execution(demo or practice) it's all the same but for a project application it completely matters to make the correct choice.

How to get the navigation bar on view controller?

I have three View controller A,B,C.I have navigation controller attached to A view controller.In A i have some buttons,I have attached the button segue to B view controller.Onclick of the button i go to the B view controller.On B View controller i have UITableView on click of table view item i am launching the C view controller.below is the code for that
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0)
{
NSLog(#"first cell");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
[self presentViewController:vc animated:YES completion:nil];
}
else if(indexPath.row==1)
{
NSLog(#"second cell");
}
else if(indexPath.row==2)
{
NSLog(#"third cell");
}
}
But on C view controller the navigation Bar is not appearing.I think C view controller is not linked to the Navigation Controller.
You use navigationController push method to display navigation bar in C viewController
try this code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
[self.navigationController pushViewController:vc animated:YES];
Use the code below
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:nav animated:YES completion:nil];
You need to present it using UINavigationController as modal.
Hope it helps.
use this:
[self.navigationController pushViewController:vc animated:YES];
You can use "Push" segue and embed your ViewController in Navigation Controller, and then use its Navigation Controller's functions like pushViewController and popViewControllerAnimated
Answer are all correct, but i just want to explain things..
//This line gets the storyboard with the name "Main" which contains all
//the setup you made for UI (User interface)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
//While this like gets the view inside your storyboard with
//storyboard ID/indentifier `BusinessCard `
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
//Lastly, this line is correct presenting the viewcontroller BUT this doesn't add your
//viewcontroller to the array of viewControllers inside navigationController
//
//Also, this line makes you present the viewController above the
//rootViewController of window which is in your case the navigationController
//
This is you Error
[self presentViewController:vc animated:YES completion:nil];
//This is what you are looking for, and the correct one for your implementation
//
//This will let you add the `vc`(viewController) to the array of viewController
//in navigationController, to confirm that you can check the `self.navigationController.viewControllers`
//which will return the array of viewController inside your navigationController
This is the Answer
[self.navigationController pushViewController:vc animated:YES];
Hope this helps you, and my explanation is apprehendable.. Cheers

Going from viewcontroller to tabbar's view programmatically

Hi, this is my storyboard , i wanna go from First view controller to LoggedinViewController.
Im normally using
[self performSegueWithIdentifier:#"s1" sender:self];
but if i add tab bar controller this line is not working , how can i fix this ?
You can use the following if you have embedded into navigation controller.
UIStoryboard *myStoryBoard =[UIStoryboard storyboardWithName:#"Story_Board_Name" bundle:nil];
UITabBarController *tabBarController = [myStoryBoard instantiateViewControllerWithIdentifier:#"TABid"];
self.tabBarController.selectedIndex = 0;
[self.navigationController pushViewController:self.tabBarController animated:YES];
Or try this
UIStoryboard *myStoryBoard =[UIStoryboard storyboardWithName:#"Story_Board_Name" bundle:nil];
UITabBarController *tabBarController = [myStoryBoard instantiateViewControllerWithIdentifier:#"TABid"];
self.tabBarController.selectedIndex = 0;
[self performSegueWithIdentifier:#"s1" sender:self];
Give your tabbarcontroller an identifier from the storyboard, the do something like this
UIStoryboard *myStoryBoard =[UIStoryboard storyboardWithName:#"Your_Story_Board_Name" bundle:nil];
TabViewController *tabBarController = [myStoryBoard instantiateViewControllerWithIdentifier:#"TAB_Identifier"];
[self presentViewController:tabBarController animated:YES completion:nil];
Hope this helps
first give "tabbarid" to that tab inside storyboard
UITabBarController *tabBarController1 = [self.storyboard instantiateViewControllerWithIdentifier:#"tabbarid"];
tabBarController1.selectedIndex = 1;
[self presentViewController:tabBarController1 animated:YES completion:nil];

presentViewController shows black page

- (IBAction)submitButtonClicked:(id)sender{
NSLog(#"here");
SuccessOrFailViewController *sfvc = [[SuccessOrFailViewController alloc] init];
[self presentViewController:sfvc animated:NO completion:NULL];
}
I am trying to open a new page when a user clicks the submit button in the app. However, this opens completely black screen with nothing on it. How do I make it open the viewController instead ?
You are not entering the nib name for the user Interface:
SuccessOrFailViewController *sfvc = [[SuccessOrFailViewController alloc] initWithNibName:#"SuccessOrFailViewController" bundle:nil];
[self presentViewController:sfvc animated:NO completion:NULL];
For storyboard this is the way to go:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
SuccessOrFailViewController *sfvc = [storyboard instantiateViewControllerWithIdentifier:#"SuccessOrFailViewController"];
[sfvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:sfvc animated:YES];
Swift
var next = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardController") as! DashboardController
self.presentViewController(next, animated: true, completion: nil)
don't forget to set ViewController StoryBoard Id in StoryBoard -> identity inspector
Incase anyone else finds this, make sure you haven't set self.view.backgroundColor = UIColor.clearColor() in the view to be shown... This solved it for me.
For Storyboard iOS7
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
SuccessOrFailViewController *sfvc = [storyboard instantiateViewControllerWithIdentifier:#"SuccessOrFailViewController"];
[sfvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:sfvc animated:YES completion:nil];
I've had this happen after adding and deleting View Controllers in the Storyboard and forgetting to update the View Controller with the latest Storyboard identifier.
For example, if you are programmatically moving to a new View Controller, like this (note Identifier in the code is "FirstUserVC"):
let firstLaunch = (storyboard?.instantiateViewControllerWithIdentifier("FirstUserVC"))! as UIViewController
self.navigationController?.pushViewController(firstLaunch, animated: true)
The make sure in Interface Builder you have the Storyboard ID set like this with FirstUserVC listed in the Identity for Storyboard ID:

Resources