How to reference a ViewController in storyboard with PresentViewController - ios

I have four tabs in a UITabBarViewController. Each view has UIButtons added to their navbar's, and each tab has a "post button" that will take the user to the post page.
How do I make reference to a ViewController that is sitting in my storyboard already so that I can pass it into the PresentViewController: method?
for example, this is the method used when the post button is clicked from any of the four tab bar views:
- (void)postInvoked:(UIBarButtonItem *)sender
{
[self presentViewController:______ animated:YES completion:nil];
}
What goes in the blank? I think I need to avoid referencing the UIViewController like this:
UIViewController * postPage = [[UIViewController alloc] init];
as this would create a new instance of the page every time the user navigates to the page.

You have to create instance of controller that you want to present:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
UIViewController *controller = [mainStoryboard instantiateViewControllerWithIdentifier: #"<Controller ID>"];
This will create controller with preconfigured views from storyboard.
#"Controller ID" == "Storyboard ID" attribute in the Interface Builder. (Inspector tab when controller is selected)
And present it as you want.
[self presentViewController:controller animated:YES completion:nil];

It depends.
A VC that is "sitting in your storyboard" doesn't exist. That's just a template that lets you create new copies of the VC when you need it.
If you already have a copy of this VC in memory, just pass a pointer t out in your call to presentViewController:animated.
If not, you need to give it a unique ID in the storyboard, and then use the UIStoryboard method instantiateViewControllerWithIdentifier: to create an instance of the VC, then pass a pointer to the newly-created VC to presentViewController:animated

1)In the story board, click on the viewcontroller you want to present then in the utilities pane, click on the identity inspector.
2)In the storyboard ID enter any string, for example "postView".
3)Edit the method to this:
- (void)postInvoked:(UIBarButtonItem *)sender
{
[self presentViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"postView"] animated:YES completion:nil];
}
In other words:
Assuming that you add the storyboard ID, the blank space should be filled in with
[self.storyboard instantiateViewControllerWithIdentifier:#"storyBoardIDHere"]
Hope this helps.

Related

Button Click To Call First Viewcontroller To Another Viewcontroller Method Using Objective C?

I have multiple UIViewController objects within one main UIViewController. I need to call FVC method when I click the main view controller button. Here three view controllers having three separate class files.
From your first controller on didSelectRowAtIndexpath method,
UIStoryboard * board = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewController * cntrl = [board instantiateViewControllerWithIdentifier:#"ViewController"];
[self presentViewController:cntrl animated:YES completion:^{
}];
add above code. Here ViewController is nothing but a your Second view controller. Using reference of cntrl pass data to Second controller.
When you want to navigate to any other controller without having navigation controller reference , you use present view controller.
If you are trying to call a method present in other ViewController without presenting it, I guess you are doing it wrong, because that method belongs that ViewController class and ideally should be called when that ViewController's lifecycle is in progress.
For your scenario, I suggest that you should create a utility class, move that method which accepts two strings and then processes something in that utility class and then call that method from your ViewController1 something like :
[UtilityClassName yourMethodWithFirstString : str1 andSecondString : str2];
Hope that clears.
UIViewController *viewVC = [[UIViewController alloc] init];
[self presentViewController:viewVC animated:YES completion:nil];
//Loading a view controller from the storyboard
UIViewController *viewVC = [self.storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];
[self presentViewController:viewVC animated:YES completion:nil];
In First View Controller
//Do this inside your btnCall method
SecondViewController * cntrl = [self.storyboard instantiateViewControllerWithIdentifier:#"secondViewControllerIdentifier"];
[cntrl methodName:firstParameter:secondParameter];
In SecondViewController
In .h file
-(void)methodName:firstParameter:secondParameter;
In .m file
-(void)methodName:firstParameter:secondParameter{
//Do your task here
}

loading a view manually objective c

Lets say I am in ViewController1 and I want to load ViewController2. The only way I know to accomplish this is using a button in ViewController1, and connecting to ViewController2 in the storyboard but I do not want to do it that way. I want to do in a method in MyViewController1. For example, in my viewDidLoad method.
Any help is greatly appreciated. Thanks
Create a segue from vc1 to vc2 inside the storyboard, give it a name and call:
[self performSegueWithIdentifier: #"mySegueCustomName" sender: self];
If you are trying to embed the view managed by ViewController2 into the view managed by ViewController1, you can do this by adding a UIContainerView to ViewController1 with an embed segue pointing to ViewController2.
This is useful, for example, if you have a reusable view managed by ViewController2 and want to use that in multiple places.
You can do this in InterfaceBuilder by dragging a UIContainerView from the Object library onto the view for ViewController1. You can then change the class of the view controller it creates to ViewController2.
Do not confuse Views and ViewControllers. You post title suggests you do not have it ingrained yet fully.
If you want to present another view controller programatically, then simply create an action method for the button and do it there. ViewDidLoad is absolutely NOT fit for that purpose.
You can present a view controller included in the storyboard like this:
-(IBAction)goToDetails:(id)sender
{
//identify the correct storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboard's name" bundle:nil];
//extract a view controller instance from the storyboard
DetailViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"details view controller's identifier"];
//present the view controller instance
[self presentViewController:detailViewController
animated:YES
completion:nil];
}
And here's an example to go a to an unrelated non-storyboard view controller.
-(IBAction)loginToSystem:(id)sender
{
LoginViewController *loginViewController = [[LoginViewController alloc] init];
[self presentViewController:loginViewController
animated:YES
completion:nil];
}

Unable to pushViewController iOS

I am using storyboard in my app, :
I have Button in my View, On click of button i want to navigate to new View
But when i click on button nothing happens,
Here is my Code:
- (IBAction)JoinClicked:(id)sender{
JoinWithViewController *detail_view_controller = [[JoinWithViewController alloc] initWithNibName:#"JoinWithViewController" bundle:nil];
[self.navigationController pushViewController:detail_view_controller animated:YES];
}
Where i am doing mistake , please help.
Thanks in advance.
Set storyboard id of view controller in story board in identity inspector
Make a reference to your storyboard like
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
then to your controller
YourController *vc = [storyboard instantiateViewControllerWithIdentifier:#"YourController"];// storyboardId
[self.navigationController pushViewController:vc animated:YES];
and you can also make segue and do
[self performSegueWithIdentifier:#"YourSegueIdentifier" sender:sender/nil];
use this one :
- (IBAction)JoinClicked:(id)sender{
[self performSegueWithIdentifier: #"JoinWithIdentifier" sender: self];
}
and add segue in storyborad like
Make an identifier for your push-segue in storyboard.
then use
[self performSegueWithIdentifier:#"YourSegueIdentifier" sender:nil];
When using story boards you should use segues for as much navigation as possible.
Your code seems fine, please verify your outlets correctly configured or not in xib or storyboard.
Edit:
You are using storyboard and wants the xib file to get pushed onto your view controller if I am not wrong, you can drag another view controller in storyboard and name it JoinWithViewController and push using segue on button click. That is much easier than what your trying to do here.

Jump to a specific view, which is already part of the storyboard, programatically

I'm trying to switch to a specific ViewController, which is part of the storyboard, but the app keeps on crashing. And I want the transition to have a "modal" style.
Note that the view that I want to switch to does not have .xib file
This is the code that I'm using:
NSString * storyboardName = #"MainStoryboard";
NSString * viewControllerID = #"GalleryViewController";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
GalleryViewController * controller = (GalleryViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
[self presentViewController:controller animated:YES completion:nil];
You can do this with segue.
First, in your storyboard, "control + drag" from the presenting view controller (the "self" in your code) to your destination controller (your GalleryViewController). This segue is not associated with any subview or widget.
Second, in the attribute inspector, give it a name "ToGalleryVCSegue", and set the type to "Modal".
Last, in the code, simply do:
[self performSegueWithIdentifier: #"ToGalleryVCSegue" sender: self];
Go to your storyboard and select your view controller. On the right select the 3rd tab. Under Storyboard Id write GalleryViewController.

Storyboard UITabBaar, initiate two different views from button

I've created a storyboard that has a UITabbarController all is working well but now I want to add some logic that determines which viewcontroller a particular tabbar button will display.
Example... if a customer has a valid subscription display viewcontroller one, if no subscription display viewcontroller two.
Is this possible using storyboards, I've looked at UITabBarDelegate and prepareForSegue but struggling to piece this together?
Are there any examples of how to do this sort of thing using StoryBoards?
Many thanks
You can set it like this:
if(hasSubscription)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
ViewController1* subsection = [storyboard instantiateViewControllerWithIdentifier:#"ViewController1"];
ViewController2* subsection1 = [storyboard instantiateViewControllerWithIdentifier:#"ViewController2"];
[(UITabBarController*)self.window.rootViewController setViewControllers:[NSArray arrayWithObjects:subsection,subsection1, nil]];
}
If you want to add rootviewController according to the subscription then above answer given by soryngod is good one.
But if you want to open viewControllers after rootviewcontroller loaded, then at tabBarButton press perform following code:-
before this code, add yours viewControllerONE and viewControllerTWO to the rootViewController by segues as shown: . And give each segue an identifier in AttributeInspector, example "one" for viewControllerONE and "two" for viewControllerTWO.
then at tabBarButton action do the following:-
if(subscription)
[self performSegueWithIdentifier:#"one" sender:self];
else
[self performSegueWithIdentifier:#"two" sender:self];

Resources