iPhone storyboard present a model view Programmatically - iphone

Am Developing a iphone appliction using this storyboard concept.
Here I have done the initial-view present using this storyboard.
Here I have created a button in rootviewcontroller programatically on user clicking on this button i should present another view controller lets say root2ViewController. Am using below code the present the view but its not presenting anything and it says like identifier not found. Any solution and idea will be appreciated.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle:[NSBundle mainBundle]];
UIViewController *root2ViewController = [storyboard instantiateViewControllerWithIdentifier:#"MyIdentifier"];
[root2ViewController setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:root2ViewController animated:animation completion:nil];

In the inspector window in you storyboard you need to set the storyboard identifier of you view controller

I Found the solution calling this below method in action is did the magic.
[self performSegueWithIdentifier: #"FindMyClinic" sender: self];

Related

Open ViewController on button click

I am new to iOS, can any one please help me to open and activity on Button click.
I have tried below methods, but the app remains on same ViewController, i am using Singleview Application type
Second *sec = [[Second alloc] init];
[self.navigationController pushViewController:sec animated:YES];
Second *sec = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondV"];
[self.navigationController pushViewController:sec animated:YES];
can anyone please help me to solve this.
if you are using storyboard then Embed your viewcontroller to UINavigationController Editor->EmbedIn->NavigationController try this
If not using Storyboard, then following might help:
Second *sec = [[Second alloc] initWithNibName:#"Second" bundle:nil];
[self.navigationController pushViewController:sec animated:YES];
For Storyboard enabled code:
Second *sec = [self.storyboard instantiateViewControllerWithIdentifier:#"YourIdentifierForVC"];
[self.navigationController pushViewController:sec animated:YES];
BTW, I strongly suggest you to google the problem for solution & search SatckOverflow - first, before posting as a new question.
Also, you should reconsider the naming for the class. Just a suggestion.
to push to another view controller you need to set a UINavigationController in front of your UIViewController in storyboard. Your self.navigationController might be nil, that's why it is not pushing your view controller.
Therefore drag and drop a Navigation Controller to your storyboard and delete the default TableViewController (make sure to click on empty space in storyboard before selecting and deleting it) and connect the UINavigationController to your own 'single view controller' by right-clicking on the yellow arrow and connecting the rootViewController outlet with it).
Then, select the UINavigationController on your storyboard and in Attributes Inspector (the fourth symbol in the right panel) under the section View Controller select Is Initial View Controller.

Getting Initial View Controller from Storyboard inside the App Delegate

I am trying to access the initial viewcontroller of my storyboard. It is a navigation controller which is wired up to a second viewcontroller via a seque. So after my application did finish launching I want to directly show the viewController that is connected through the segue. I tried it out with code like this but it doesn't work ...
UINavigationController *navController = (UINavigationController*)self.window.rootViewController;
[navController.topViewController performSegueWithIdentifier:#"showLoginScreen" sender:self];
What am I doing wrong?
4 years later:
If I look at my question again after 4 years, I honestly have no idea what my real problem was.
Be sure you have tick marked the
is initial ViewController
option for UINavigationController in storyBoard
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UINavigationController * myStoryBoardInitialViewController = [storyboard instantiateInitialViewController];
Sebastian, I'm not sure why you would want to start the initial view controller manually - all my projects (which all use storyboards) do this automatically if you ticked "Use Storyboards" on the second screen in the "New Project" wizard.
Maybe you need to mark the storyboard scene as inital? This can be done in the scene's attribute inspector by ticking the "Is Initial View Controller" - rather obviously named.
And then - if you really have a unusual setup which requires you to access the scenes manually, you can use the following:
UIStoryboard *sb = [UIStoryboard storyboardWithName: "StoryboardName"
bundle: [NSBundle mainBundle]];
UIViewController *vc = [sb instantiateInitialViewController];
(Beware - no code completion here, so check spelling again.)
Or maybe I am getting your question completely wrong..? Happy hacking!
In my app i have tabBar
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
TabBarController *tabBar = [storyBoard instantiateViewControllerWithIdentifier:#"TabBarController"];
_window.rootViewController = tabBar;
Ok, here is a reason in the year 2022 pre SwiftUI apps. Your apps starts... it has several choices, one, it wants you to login, two it wants you to sync with home base, and three everything is fine and just show the main UI. Case two may refresh a rather large body of information before you build out you main UI.

Navigating to a TabBarController from a ViewController in iOS 5 using storyboard

I started developing my app with a TabBarController but I recently realized the use of authenticating the user before accessing the app. So I created a login page before TabBarContoller(2 tabs and 2 views). But now I am not able to push the controller from ViewController to TabBarController.
What, I have tried so far is: (After the user taps the submit button on login page):
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController* vc = [sb instantiateViewControllerWithIdentifier:#"twoOptions"];
[self.navigationController pushViewController:vc animated:YES];
Here, twoOptions is the identifier I have named in the Interface Builder for the TabBarController in StoryBoard
Please guide me
I went through the following link:
ViewController to TabBarController but still did not understand what needs to be done. I am unable to push the view
Here is the Link for the example.
UITabBarController *tabBarController=[[UITabBarController alloc]init];
tabBarController.viewControllers = [NSArray arrayWithObjects:firstViewController,secondViewController,thirdViewController, nil];
[self presentModalViewController:tabBarController animated:NO];
I don't know know why are you using storyboard, but it can be easily done with the code also.

Looking to maximize reuse in an iPhone storyboard application

I'm programming a complex storyboard application and I'm encountering some issues when it comes to code re-use across view controllers and different application paths.
Can I segue to a ViewController that is not directly connected to the current one?
How do I segue out from a button to several ViewControllers conditionally? Connecting both does not work.
Can I enter the ViewController sequence from an arbitrary position in the application?
Just a few questions that come up. Anybody have any ideas or good examples?
Can I segue to a ViewController that is not directly connected to the current one?
Not with a segue. But you can push or present a viewController from a storyboard modally, like you did with .xib files.
// if self was created from code or from a .xib:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryBoard" bundle:nil];
// if self was instantiated from within a storyboard:
UIStoryboard *storyBoard = self.storyboard;
MyFancyViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"MyFancyViewControllerIdentifier"];
[self presentModalViewController:viewController animated:YES];
But you have to set the identifier first. Select the viewController in the storyboard and do that in the attributes inspector.
Once the MyFancyViewController is visible you can use all its segues. It might feel strange to switch between code and storyboard segues, but there is nothing wrong with that. It makes the whole storyboard thing really usable.
How do I segue out from a button to several ViewControllers conditionally? Connecting both does not work.
Add two or more segues that start from the viewController (not from a button or any other view. E.g. start your control-drag at the statusbar) to the target viewControllers. Set identifiers for them and use some code like this:
if (someState) {
[self performSegueWithIdentifier:#"MyFirstSegueIdentifier" sender:somethingOrNil];
}
else {
[self performSegueWithIdentifier:#"MySecondSegueIdentifier" sender:somethingOrNil];
}
Can I enter the ViewController sequence from an arbitrary position in the application?
Yes, if you use "old school" view controller management. Similar to the first part. Instantiate your viewController and present it with code.

iOS - Interface Builder Outlets Not Initialized

I have created a view in Interface Builder with some labels and text as IBOutlets. I can view this screen perfectly when I segue to it from another view that I have defined in my Storyboard.
However, I have another XIB and an associated UIViewController that I want to access that view from. Because my XIB is not in the Storyboard, I cant segue to it. Instead I have to execute the transition programmatically.
PlantDetailViewController *plantDetailVC = [[PlantDetailViewController alloc] init];
[self.currentNavigationController pushViewController:plantDetailVC animated:YES];
When this code is executed it transitions to the view but the view is just blank. I have debugged the code and it enters viewDidLoad and viewWillAppear however all my IBOutlets are NIL....so nothing it showing up on screen!
Can anyone tell me why they might be NIL and how I can initialize them?
Thanks
Brian
It sounds like you're saying you have a PlantDetailViewController set up in your storyboard, and some OtherViewController that was created outside of your storyboard. Now you want OtherViewController to instantiate the PlantDetailViewController that was set up in your storyboard.
Let's say your storyboard is named MainStoryboard.storyboard.
First, you need to set the identifier of the PlantDetailViewController in your storyboard. You do this in the Attributes inspector, under the View Controller section. Let's say you set it to PlantDetail.
Then, in OtherViewController, this should work:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
PlantDetailViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"PlantDetail"];
[self.currentNavigationController pushViewController:vc animated:YES];
-init doesn't load a nib file for you, if you want to load a nib use -initWithNibName:bundle:
If you use nib naming conventions you can pass nil to load a nib whose name matches your class and the default bundle, i.e. [[PlantDetailViewController alloc] initWithNibName:nil bundle:nil], see the docs for -nibName for details.