I have 2 storyboards. I have a situation where I have to navigate from a view controller1 in storyboard 1 to a view controller2 in storyboard 2. The view controller 1 is added as a subview to a parent view controller. When I click on a row in a table view in view controller 1, I have to navigate to storyboard 2.
Initially it was not detecting a touch in the table row. I managed handling it by using a single tap gesture. Now when I click on a row, I am able to navigate to storyboard2 viewcontroller 2 modally. But after navigating ,I am not able to move anywhere on a button click in view controller 2 because it uses all push segues there.
Here is the sample code which I used to navigate.
*NSString * storyboardName = #"OppotunityStoryboard";
NSString * viewControllerID = #"AgreementMainViewController";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
AgreementMainViewController *controller = (AgreementMainViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
controller.productTransactionentity = productTransactionEntity;
[self presentViewController:controller animated:YES completion:nil];*
You have to embed ViewController 2 in a navigation controller in order for it to be able to use push segues when presented modally.
Related
In Periscope Live Broadcast app, when you click on WORLD tab bar and then tap on "List" and you tap to watch a live stream, the Live stream view controller will pop up from the bottom covering the entire tab bar view controller with the tab bar completely hidden.
(I'm currently using Tab Bar Controller and Navigation Controller and I can't get it done)
How is this done?
You can call your "modal" view controller programmatically:
NSString * storyboardName = #"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];
[self presentViewController:vc animated:YES completion:nil];
IDENTIFIER_OF_YOUR_VIEWCONTROLLER is the name you set in your storyboard. You can assign in the identity inspector of your storyboard.
This view will cover your screen as a modal
I added a Navigation Controller to my storyboard and it appears like so:
Now in the table view controller, I gave the TableViewController a storyboard id and class to a TableViewController Controller
When I run my app, I don't see the Navigation Bar at the top. This has been extremely frustrating and can't find a solution anywhere. PLEASE HELP
To get to the scene, someone clicks a button and this code runs and it goes to my Table View Controller:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
LHFileBrowser *LHFileBrowser = [storyBoard instantiateViewControllerWithIdentifier:#"FileBrowser"];
[self.navigationController pushViewController:LHFileBrowser animated:YES];
[self presentViewController:LHFileBrowser animated:YES completion:nil];
The error is in your code.
If you want to (modally) present a view controller when the user presses a button, you need to present the navigation controller (which will contain the table view controller), not the table view controller itself.
Right now, you're presenting the view controller, which won't show it being embedded in a navigation controller.
Also, you're mixing up two different approaches, by trying to push a view controller onto a navigation controller stack, and also presenting the view controller.
Code Sample:
Here's what you apparently mean to do:
UIStoryboard *storyboard = self.storyboard;
UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:#"MyNavigationControllerID"];
LHFileBrowser *rootViewController = [navigationController topViewController];
// Configure your LHFileBrowser view controller here.
rootViewController.someProperty = ...;
// Modally present the embedded view controller
[self presentViewController:navigationController animated:YES completion:nil];
If you want to change the presentation or transition style, you can set those details in your storyboard.
You didn't explain why you had to programmatically add buttons, but Storyboard segues would have instantiated and presented an embedded view controller for you, without you having to have done it in code.
The more you can do in Storyboard, the less code you have to maintain, support, and update, and the more likely your app will still work properly when a new SDK is released.
Update:
The better way to do this is to let Storyboard do it for you, by adding a segue from the button to the navigation controller that you want to present.
In my app I've three view controller embedded in a UITabBarController. The second view controller embed a table view controller. Now I've this bug: I go to the second view controller, then I press home button and the app goes in background. When the app is closed I receive a push notification, so when I open the app it shows me the second view controller with table view without any changes. How I can update the table view automatically? For now I'm using the UIRefreshControl, but I will do it automatically, how I can do that?
You can get your VC's instance from storyboard. First you have to set storyboard ID to your VC
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
YourViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:#"YourViewControllerID"]; // here YourViewControllerID is storyboard Id of your VC.
I have two storyboards in the same ios application.
Storyboard 1 is the login.storyboard.
Storybaord 2 is the processing.storyboard.
login.storyboard has the following scenes:
1) welcome
2) login
processing.storyboard has
1) start
2) images
3) description
4) finish
login.storyboard handles login whilst processing.storyboard creates and object for uploading.
My understanding of the stack is as follows:
Navigate from welcome to login gives:
1:[welcome]-[login.storyboard]
2:[login]-[login.storyboard]
after login I push the processing.storyboard using
- (void) pushStory: (NSString *) story {
UIStoryboard *settingsStoryboard = [UIStoryboard storyboardWithName:story bundle:nil];
UIViewController *initialSettingsVC = [settingsStoryboard instantiateInitialViewController];
initialSettingsVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:initialSettingsVC animated:YES completion:nil];
}
the stack should now be:
1:[welcome]
2:[login]
3:[start]
I might get to description in the workflow before I decide to click logout (available on each page), my stack at this point would be
1:[welcome]-[login.storyboard]
2:[login]-[login.storyboard]
3:[start]-[processing.storyboard]
4:[images]-[processing.storyboard]
5:[description]-[processing.storyboard]
logout should take me back to [welcome], my question is with the storyboards how would I clear the stack back to [welcome] and ensure the login.storyboard is current.
There's a gap in my knowledge here, as I've just gotten back into iphone dev after 6 years or so and not seen these before.
I had thought of just pushing login.storyboard onto the stack but that would just make the stack continue to grow instead of clearing it out
Use unwind segues.
Add this method in [welcome]:
-(IBAction)reset:(UIStoryboardSegue *)segue
{
NSLog(#"Back to Welcome");
}
In Interface Builder, create UIButtons in [start], [images] and [description] then link each of these buttons to the green "Exit" button of their respective viewControllers and select reset: in the pop-up menu that appears.
(See WWDC 2012 session video "Adopting Storyboards in Your App" for more details about unwind segues [starts at 38 minutes].)
You can pop back to any arbitrary point in the stack, for example
[self.navigationController popToRootViewControllerAnimated:YES]; // all the way back to the first view controller
[self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES]; // back to the second view controller
[self.navigationController popViewControllerAnimated:YES]; // back to the previous view controller
This answer assumes that storyboard 1 has
navigation controller
welcome view controller
login view controller
and storyboard 2 has
navigation controller
start view controller
images view controller
description view controller
finish view controller
Note that the navigation controller in storyboard 2 is never actually instantiated but is needed so that the other view controllers in storyboard 2 can be connected by segues. When navigating from the login view controller to the start view controller the code should look similar to this
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"SecondStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"ControllerC"];
[self.navigationController pushViewController:vc animated:YES];
Note that this doesn't instantiate the initialViewController since that's the navigation controller, and we don't want another navigation controller. Instead, give the start view controller a Storyboard ID under the Identity inspector, and then instantiate the start view controller directly. After instantiating the start view controller, push it onto the existing navigation controller. You may want to hide the back button if you don't want the user to navigate back to the login view controller.
I am having a problem with this.
In my root view controller I am having a textfield & one button. I am giving a condition like if i entered 0 in textfield then only it should move to next view.
upto here it is working correctly. But now here is problem. Here I am having one button & given navigation to third view controller for that button. here i am getting error as
Terminating app due to uncaught exception 'NSGenericException', reason: 'Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
image ref: http://img12.imageshack.us/img12/8533/myp5.png
and i am giving action for button in first view as below
- (IBAction)Submit:(id)sender {
if([tf.text isEqual:#"0"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SecondViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewControllerID" ];
[self presentViewController:vc2 animated:YES completion:NULL];
}
}
Go To:
Editor--> Embed In --> Navigation Controller, to add Navigation Controller to your initial view
After looking at your screenshot, you either need to
Push your SecondViewController onto the existing navigation controller's stack instead of presenting it modally OR
You need to embed your SecondViewController in another navigation controller and then create and present that navigation controller modally from your SamplesViewController
Either way, SecondViewController needs to be embedded in a navigation controller before you can use Push Segues from it
Either embed your view controller in a Navigation controller or if there is a Navigation controller in the story board mak it the initial view controller
I also faced same problem. My problem was I was using model segue style (rather that push) for one before the current controller because of that I think it broke the Navigation chain before already.
Embed your view controller in a UINavigationController is a good option if there is no UINavigationController in the storyboard. Otherwise you can select the UINavigationController and select the root view controller in the inspector pane, then drag it to the UIViewController you want to use as root. the screenshot is as below:
I solved this by creating a custom UIStoryboardSegue between the UINavigationController and the destination view controller:
-(void) perform
{
assert([self.sourceViewController isKindOfClass:[MyNavigationController class]]);
MyNavigationController *launchController = (MyNavigationController *) self.sourceViewController;
[launchController setViewControllers:#[self.destinationViewController] animated:YES];
}
Try this. It works for me.when you set root view controller to first view and use self.presentViewController ,controller move to next view but instance of navigation controller is only for first view,third view required navigation instance so use self.navigationController instead of presentViewController.
NSString * storyboardName=#"Main";
UIStoryboard *storybord=[UIStoryboard storyboardWithName:storyboardName bundle:nil];
SecondViewController *vc=[storybord instantiateViewControllerWithIdentifier:#"SecondViewControllerID"];
[self.navigationController pushViewController:vc animated:YES];