I'm trying to create a manual segue, which happens only if there is a certain conidition (a successful login).
The steps I have followed are these:
1) I've created a button in source viewcontroller, and i connected the button with a drag with the destination viewcontroller.
2) I set the identifier for the segue, calling it login_ok
3) I've created a -(IBAction)buttonPressed:(id)sender
Now, I tried to make an example, to see if the work or not. I wrote:
- (IBAction)buttonPressed:(id)sender
{
BOOL test = false;
if(test == true)
{
[self performSegueWithIdentifier:#"login_ok" sender:self];
}
Theoretically, the segue should not work, because i set test=false, and i said the segue should work only if test=true.
But the segue works and makes me go to the destination viewcontroller.
Why?
If you connected the button with a drag to the destination controller, it should segue anyway.
You need to connect the segue between screens and not from the button to the screen. Then, you can control your segue from the code.
You should define your segue between controllers not from button to the controller. Defining segue between view controllers is more useful than defining segue from any component to a view controller, because when you want to use a condition for any action, you can use these two methods effectively
[self performSegueWithIdentifier:#"segueID" sender:self];
and
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
For example:
- (IBAction)buttonPressed:(id)sender
{
BOOL test = YES;
if(test == YES)
[self performSegueWithIdentifier:#"login_ok" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"login_ok"])
{
YourNextController *controller = [segue destinationViewController];
controller.welcomeText = #"Hi! Test is true congratulations";
}
}
Especially passing data from controller to another one, this way is always useful.
Related
I'm new on Stackoverflow and I'm currently learning XCode from scratch and I'm in a process of making a Single Page Application with options.
Anyone knows how to efficiently make a simple menu with multiple selectable UIButtons that make the main ViewController display different datasets depending on the selection in XCode?
Tried different things (creating SecondViewController for example but can't figure out how to pass data from it to main ViewController).
Any help will be greatly appreciated.
Refer this:
https://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/
How to pass prepareForSegue: an object
Simple Test from above Answer Reference:
Simply grab a reference to the target view controller in prepareForSegue: method and pass any objects you need to there. Here's an example...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
REVISION: You can also use performSegueWithIdentifier:sender: method to activate the transition to a new view based on a selection or button press.
For instance, consider I had two view controllers. The first contains three buttons and the second needs to know which of those buttons has been pressed before the transition. You could wire the buttons up to an IBAction in your code which uses performSegueWithIdentifier: method, like this...
//When any of my buttons are pressed, push the next view
- (IBAction)buttonPressed:(id)sender
{
[self performSegueWithIdentifier:#"MySegue" sender:sender];
}
// This will get called too before the view appears
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"MySegue"]) {
// Get destination view
SecondView *vc = [segue destinationViewController];
// Get button tag number (or do whatever you need to do here, based on your object
NSInteger tagIndex = [(UIButton *)sender tag];
// Pass the information to your destination view
[vc setSelectedButton:tagIndex];
}
}
Hope this help but please go through refernces!
So I know that if there is a segue which is setup and activated through the storyboard, one can use the prepareForSegue method to pass data to the destination view controller.
I'm wondering if there is a way to do this if one uses the performSegueWithIdentifier method? It seems the prepareForSegue method is only called if the segue is activated with the storyboard, not with performSegue.
One particular problem is that I can't access the UIStoryboardSegue (it is not an argument in performSegue as it is in prepareForSegue), and therefore can't access the destination VC either.
Answer is here in StackOverflow.
How to pass prepareForSegue: an object
// When any of buttons are pressed, push the next view
- (IBAction)buttonPressed:(id)sender
{
[self performSegueWithIdentifier:#"MySegue" sender:sender];
}
// This will get called too before the view appears
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"MySegue"]) {
// Get destination view
SecondView *vc = [segue destinationViewController];
// Get button tag number (or do whatever you need to do here, based on your object
NSInteger tagIndex = [(UIButton *)sender tag];
// Pass the information to your destination view
[vc setSelectedButton:tagIndex];
}
}
Upon clicking on the button, I want the user to navigate to another view. The following code works fine. However, I want to pass some values as well to the next screen. How am I suppose to do so ?
- (IBAction)buttonClick:(id)sender {
[self performSegueWithIdentifier:#"segueviewc" sender:self];
}
I also tried the following code and it works but the page navigates twice to the MyV viewcontroller. From buttonClick event and from prepareForSegue method. How can I prevent this ?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
MyV *myv = (MyV*) segue.destinationViewController;
myv.navigationItem.title=#"Hey";
myv.arr=data;
}
Open the storyboard check if the segue is directly connect from the button to the next view. If it does, you can simply delete the line.
[self performSegueWithIdentifier:#"segueviewc" sender:self];
i think the problem is, the segue is directly connected from the button action to the next view, and within the button action, you call the segue again!
cheers
You don't have to perform the segue on your button click. If your button is triggering the segue, it will be performed automatically.
Remove the line -
[self performSegueWithIdentifier:#"segueviewc" sender:self];
from your method -
- (IBAction)buttonClick:(id)sender {
Also you must ideally check which segue is being performed, so that you can code according to the navigation being performed...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.idenifier isEqualToString:#"segueviewc"])
{
MyV *myv = (MyV*) segue.destinationViewController;
myv.navigationItem.title=#"Hey";
myv.arr=data;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"segueviewc"]) {
MyV *myv = (MyV*)[self.storyboard instantiateViewControllerWithIdentifier:#"yourStoryboardID"];
myv.navigationItem.title=#"Hey";
myv.arr=data;
}
}
And you do not need IBAction method.
There are 2 ways to trigger a segue:
1. You trigger it as an action from your button directly to the new view controller
2. You trigger the segue from one view controller to the second view controller.
I am guessing you are connecting it via 1st approach. Hence you should not trigger it from IBAction.
Hello I have a button that must present a modal view controller when tapped
Here is the action:
- (IBAction)addNewLevelAction:(id)sender
{
[self performSegueWithIdentifier:kNewLevelConfigureSegue sender:self];
}
in prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:kNewLevelConfigureSegue]) {
PSLevelConfigViewController *dest = (PSLevelConfigViewController *)[segue destinationViewController];
dest.delegate = self;
}
}
However when I tapp it I get:
Warning: Attempt to present <PSLevelConfigViewController: 0x98cbd00> on <UINavigationController: 0x98aac70> while a presentation is in progress!
Why is this? There is no other presentation there...
If you perform the segue programmatically, the segue must be connected to the controller and not directly to the button.
Check if your storyboard is set correctly (post some screenshots if you need help).
In such situation, if your segue is connected to the button AND, at the same time, to an action that performs it programmatically, your segue will be performed twice, and the second perform will cause that error.
Then I call this method I want to have segue. Is it right?
- (void)showMapViewController {
[self performSegueWithIdentifier:#"MapViewController" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"MapViewController"])
{
UINavigationController *navigationController = segue.sourceViewController;
LoginViewController *loginViewController = [[navigationController viewControllers] objectAtIndex:0];
[loginViewController performSegueWithIdentifier:#"MapViewController" sender:self];
}
}
You have to create segue in storyboard and give a unique identifier. It can be perform automaticaly if you set this on some button action, e.g. if you create a segue on button action an want to push a navigation controller. When ever that button will be push segue will be perform automaticlay. In this case if you want to do some custom coding to set some properties etc. then you should implement this delegate method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"segueIdentifier"])
{
// Here you can get source and destination view controllers and can perform some custom tasks.
}
}
You can also perform segue by code if you want to, for that you have to call this method.
[self performSegueWithIdentifier:#"segueidentifer" sender:self];
For this you have to create a segue in storyboard by clt+drag from one view controller to other(source -> destination) and give an identifier.