Search code examples
iosuibuttonuipickerview

How do I change the action of a button with UIPickerView


I am trying to change where a button leads to depending on the selected row of a picker view. I am trying to display data depending on a certain year and I can make the picker display the available years and change a label to the corresponding year, but how can i make the "go" button I;ve created lead to the correct view controller?


Solution

  • Maybe you haven't had time to figure this out, maybe you have, but this is what I would do. You have a text field that you will populate with a picker, call this the "yearTextField." Then you have a button that you want to change the action depending on the year in question. Call this "myChangableButton." This is just for an example, you can call them anything you like. I put my initial view controller in a navigation view controller to have better control of the views. For this example I made two different years 1942, and 1971. They each have their own view controller that I named "FourtyTwoViewController" and "SeventyOneViewController" in my first view controller you will need to import these into the .m file. Like this..

    #import "ViewController.h"
    #import "FourtyTwoViewController.h"
    #import "SeventyOneViewController.h"
    

    Also, make sure when you drag out the new view controllers in the storyboard, that you name them as such. And don't forget to use that name as the Storyboard ID, found under the Identity inspector. Now for myChangableButton I made an action that will bring up whichever view controller you want based on the text in the yearTextField like this..

    - (IBAction)myChangableButton:(id)sender
    {
        if ([_yearTextField.text isEqual: @"1942"])
        {
            FourtyTwoViewController *ftvc = [self.storyboard instantiateViewControllerWithIdentifier:@"FourtyTwoViewController"];
            //[self presentViewController:ftvc animated:YES completion:nil];
            [self.navigationController pushViewController:ftvc animated:YES];
        }
        else if ([_yearTextField.text isEqual: @"1971"])
        {
            SeventyOneViewController *sovc = [self.storyboard instantiateViewControllerWithIdentifier:@"SeventyOneViewController"];
            //[self presentViewController:sovc animated:YES completion:nil];
            [self.navigationController pushViewController:sovc animated:YES];
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Watch Out!" message:@"Put in valid date" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
    }
    

    The line of code that I commented out will bring up a modal transition, but I prefer a push since that gives you a great back button. That's it. I hope this helps you on your project. Make a comment if you have any questions, and good luck. And WELCOME! to SO!!

    EDIT #1

    Here is a sample of how I set the initialUrl in a viewcontroller that I pushed.

    VideoViewController *vvc = [self.storyboard instantiateViewControllerWithIdentifier:@"VideoViewController"];
    vvc.initialUrlString = urlString;
    vvc.descriptionString = descriptionString1;
    [self.navigationController pushViewController:vvc animated:YES];
    

    I imported VideoViewController into the first view controller. And VideoViewController has two properties called initialUrlString and descriptionString. In the first view controller I have two properties called urlString and descriptionString1, these are set in the first view controller during another method and then passed into the second.