I'm trying to create a dead simple storyboard app with a layout similar to the following. I've got the list/detail view setup but I can't figure out how to link additional pages. I want the "go" page to be my start page and drive off that.
Can someone help me with a dead simple example solution on how you would set this up? I'm trying to use "push" segues.
Here are different ways to create a segues:
1 - From Control To Controller:
2 - Using Connection Inspector
3 - From View Controller to View Controller
#1 and #3 you will need to hold control before
dragging.
#2 & #3 :
You will need to give an identifier to your segue, unlike #1 You have to performe the segue using code:
add identifier:
Perform the segue:
[self performSegueWithIdentifier:#"yourSegue" sender:sender];
Now here is the thing, this will just perform the segue, if you ever needed to pass some data to that view controller. Then you have to implement the following segue deleguate:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"yourSegue"])
{
//if you need to pass data to the next controller do it here
}
}
Push segues will not work until you embed your view controller hierarchy in a UINavigationController. You can place one as root view controller for your GO view controller and then it should work
You can do this for example by ctrl click on a button and then move the mouse to your target view controller and release.
I've created a very simple example in my blog:
http://stefansdevplayground.blogspot.de/2014/02/howto-add-view-controllers-to.html
Related
I'm trying to execute code when a button is pressed for an application but I can't find how to change the views after the code is executed. Is there a way to switch views how I want to or is there another way? Thank you in advanced, I'm very new to xcode.
edit: I'm trying to go from one view to another, not the view controller and yes I have one storyboard that I planned on using for the whole project if possible.
To execute code when a button is pressed, you have to set up a method that the button is hooked into. Because you said you're using storyboards, I'll assume your button is on the storyboard. Go to the assistant editor, hold ctrl, and click-and-drag from the button to the view controller's .m file (#implementation section). This will create an IBAction method, and any code in this method will execute whenever you press the button.
The method will look like this:
- (IBAction)aButtonPress:(id)sender {
}
According to your comments, you say you only want to change the on-screen view, and not transition from one view to the next.
Views are added and removed with the following methods:
[aSuperview addSubview:aSubview];
[aSubview removeFromSuperview];
I can't really tell you much else with a lot more detail from you... and even though I asked twice in the comments and you said you only want to change the view, not the view controller... I think you probably need to transition to a new view controller. Impossible to know for sure as you've given almost no detail...
But if you want to transition between view controllers (which also transitions views), then ...
Create two view controllers on the storyboard. Hook them together with a segue, and give that segue a name. Now, to perform that segue in code:
[self performSegueWithIdentifier:#"YOUR_SEGUE_NAME" sender:self];
Is there a way to connect a prototype cell to different scenes and on the didSelectRowAtIndexPath method call the [self performSegueWithIdentifier:#"NAMEOFTHESEGUE" sender:self] ?
I can't seem to drag multiple segues from the prototype cell to different scenes... I know it is possible with static cells because you can draw a segue from each cell to the scene but what if i'm using prototype cells?
I'm currently trying to implement this using the SWRevealViewController (https://github.com/John-Lluch/SWRevealViewController) plugin.
In the storyboard example project he's using a table with 3 static cells and connecting each cell to a different scene. But in my case static cells are not an option so if anyone knows the answer that would be great.
Oh and also embedding the "side panel view controller" which is a UIViewController inside a navigation controller so i can connect the navigation controller to different scenes is not an option because in that case the new scene opens inside the slide panel ...
Please take into consideration that i'm still a rookie in IOS programming.
Appreciate your time!
EDIT + solution : I had tried connecting the segues from the view controller to the scenes but it kept giving and exception "NSInvalidArgumentException - receiver has no segue with identifier..." The solution was in Xcode to go to "Product" then choose "Clean" : https://stackoverflow.com/a/14089463/1185133
Yes, the only thing is that you don't have to connect your prototype cell to multiple destinations, the origin of the multiple segue must be the view controller:
1 - create the segue from your Table View Controller (not the cell) to the destination VC and give to each segue an unique name.
2 - override didSelectRowAtIndexPath: and call [self performSegueWithIdentifier:#"NAMEOFTHESEGUE" sender:self]
That's all :-)
If you have you have put a table view within a View Controller, drag the segues from the Controller and not the cell, just as LombaX has explained above
I am building an App that uses three UIViewControllers. I am using navigation controller to navigate. First view controller (v1) will have two text boxes and a button. On the button click, I have a function that checks certain criteria and if its satisfied then I move to the second view controller (v2) or else I want to move to the third view controller (v3). How is it possible through code?
One last thing, I want to capture these text box values and use them in the appropriate view controller. How is it possible?
In terms of programmatically transitioning to another scene in your storyboard, the general procedure is:
define a segue between the view controllers (not the button, but the view controllers);
give that segue a storyboard identifier;
create an IBAction for a button that does the validation, and if successful, does a
[self performSegueWithIdentifier:#"myidentifier"];
See this answer on stack overflow in which I walked an individual through this process in some detail. If your case, it sounds like you're validating data, and might perform one segue given one series of conditions, and another segue in another series of conditions. So just give those two segues unique identifiers, and then you can perform whichever one your want in your IBAction code.
In terms of passing data to that next scene, you do this in a separate method, prepareForSegue, which iOS calls after the destination controller has been created, but before it's been presented. It's here that you have a chance to pass parameters to the destination controller. For example, I'm assuming that I captured the first and last name of an individual and want to pass that to the destination controller, which has first and last name properties:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"myidentifier"])
{
MyDestinationController *controller = segue.destinationViewController;
controller.firstName = self.labelFirstName.text;
controller.lastName = self.labelLastName.text;
}
}
As of iOS 6.0, you can implement -shouldPerformSegueWithIdentifier:sender: in your controller. Much simpler than what you had to do in iOS 5.
Am a newbie to App development. I created a Navigation Controller template project and added few pages to it. It looks like below:
MY QUESTION
When the App launches, I want the 'Home' page (which is the first ViewController in the list) to be shown by default. And then, if the users want to get to this Menu screen (i.e. the list of controllers), they need to be able to press the 'Back' button (which would appear in the top navigation bar).
WHAT I TRIED OUT
I tried this by pulling the '--->' arrow mark (which now points to the Navigation controller) to the ViewController that represents the 'Home' screen. As expected, when I ran the app, the page representing the 'Home' screen was shown first. However, the 'Back' button was missing in the Navigation Bar (it's obvious why, though) and so one cannot goto the Menu page.
Can someone please pour in your expertise!
Thanks,
Jean
You can use the method below in viewDidAppear:animated method
[self performSelector:#selector(performSegueWithIdentifier:sender:) withObject:#"MainRootView" afterDelay:2.0f];
here #"MainRootView" is identifier defined in storyboard in segue, used to call or load the view and the performSegueWithIdentifier:sender: method is as follow
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(#"Segue about to be performed");
}
this will load the view who's segue identifier is #"MainRootView".
May this help you.
Happy Coding :)
I am creating a project with Xcode 4.2 and using it's storyboard. In one of the views I have a button that a user will tap and it will perform some calculations. If the calculations are correct I need to display the view that they just came from. I am currently using a Navigation Controller. When the app starts it loads View 1. When I choose an option it loads View 2. If I click the 'back' button in the toolbar it loads View 1 with left animation.
In my IBAction method for the View 2 calculation I have the following snippet.
[self performSegueWithIdentifier:#"BackToView1" sender:sender];
But the problem is it loads View 1 using the left animation when I need it to load the right.
Would that be a custom segue? Am I missing something?
[Edit]
I just noticed something as well. When View 1 loads from my IBAction it appears it is initializing the AppDelegate again, whereas the 'back' button does not. I need to not initialize the AppDelegate since I am loading a data object at the start of the app. Calling init again kills my object.
You could try using the navigation controller to pop back. This will probably produce the effect you are after.
[self.navigationController popViewControllerAnimated:YES];