I have a push segue and want to transfer data between views. I need a different way to resign first responder if the user taps the next button in the navigation bar. I have [textField resignFirstResponder]; in the navigation bar button method but for some reason it is called after the next page viewDidLoad.
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:[NSString stringWithFormat:#"thridClubPage"]]) {
thirdClubPage *nextPage = segue.destinationViewController;
nextPage.title = self.navigationItem.title;
nextPage.location = self.collegeString;
nextPage.people = self.membersString;
nextPage.leader = self.presidentString;
nextPage.Email = self.presidentEmailString;
}
}
- (IBAction)finishPage:(id)sender {
[self.presCell.presidentEmail resignFirstResponder];
}
the segue performs without resigning the textField.
A quick solution is to delete the segue from the button in IB and redraw it between the two view controllers. (In other words, when dragging out the segue, don't control-drag from the button, control-drag from the view controller that contains it). Give the segue an identifier, like #"NextSegue".
Now you can control the order of what happens by triggering the segue yourself...
- (IBAction)finishPage:(id)sender {
[self.presCell.presidentEmail resignFirstResponder];
[self performSegueWithIdentifier:#"NextSegue" sender:self];
}
Related
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.
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.
I have this problem:
I have two ViewControllers.
I am transitioning to the second view with Segue.
User enters his name on first view controller and taps on a button. If the text is nil, it should not show the second view controller. If some text is there on the text field - it has to show the next view controller.
I am checking the text length here below. As I have only one segue... I am not checking segue identifier.
And, on Storyboard, I have give modal transition CoverVertical. The animation is not working. View is just appearing. UIModalTransitionStyle also I tried. Still not working (on device and simulator)
-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
if(playerNameTextField.text.length == 0)
{
UIColor *tempColor=UIColorFromRGB(0xFF4981);
[self colorizeTextViewForAWhile:playerNameTextField withUIColor:tempColor animated:YES];
return NO;
}
return YES;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"segueMoveToHome"])
{
ViewController *vc = (ViewController *)segue.destinationViewController;
vc.playerName=playerNameTextField.text;
}
}
Your code works perfectly. I just reproduced and I've got a transition with CoverVertical.
In Interface Builder, select the Storyboard Segue and be sure that you have:
I'm linking you a mini demo with your code that works.
I have removed the Identifier text and continued. As it was the only segue or any other reason... it was working. If I put the segue... it is not working. :| Not sure. But I have now my app working.
I have a simple storyboard file with a table view inside a navigation controller. If you cick one of the cells it goes to the details view. Great.
Then I tried adding a toolbar button and hooking it up to my table view controller. The button and the seque seems to work fine, except now, when i click on one of the table cells, the modal view is triggered.
So it seems I have done something wrong, but I don't understand what.
// called by pressing the add button in the toolbar
- (IBAction)showAddEventView:(id)sender {
[self performSegueWithIdentifier:#"showAddEvent" sender:sender];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showAddEvent"]) {
AddEventVC *addEventVC = [[AddEventVC alloc] init];
// set some property
} else {
EventIndexVC *eventIndexVC = [segue destinationViewController];
eventIndexVC.eventTitle = [self.events objectAtIndex: [[self.tableView indexPathForSelectedRow] row]];
}
}
Okey guys, here's the deal...
I'm trying to connect two views with one segue and writing in Label a tag from clicked button. I hope you know what I mean...
Here's the example:
Title view appears and you have to choose the difficulty 1-3 (tag values of buttons) and after clicking your're moved to game view but you'd like to use tag values from title view buttons in game logic.
Here's the sample screen:
This is what I've found and this perfectly suits!
// When any of my buttons are pressed, push the next view
- (IBAction)buttonPressed:(id)sender
{
[self performSegueWithIdentifier:#"Segue" sender:sender];
}
// This will get called before the view appears
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"Segue"]) {
// 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];
}
}
And here is the link to whole topic.
Set your buttons to trigger the segue that presents the second view controller.
In your first view controller, implement - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender and use the sender to retrieve the tag.