0
votes

So I have a UIPickerView and UIButton.

Basically I have built it so the images change when the options are scrolled through. I now want to add a submit button (UIButton) so when tapped it sends them to a web address for more information on the option selected.

Here is my code so far.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // Load up NSArray
    _characters = [[NSArray alloc] initWithObjects:@"bergerac" , @"biarritz" , @"chareleroi" , @"liege" , @"oostende" , nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIPickerView Methods

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _characters.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [_characters objectAtIndex:row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //Create UIImage
    UIImage *bergeracimage = [UIImage imageNamed:@"Bergerac.JPG"];
    UIImage *biarritizimage = [UIImage imageNamed:@"Biarritz-Anglet-Bayonne_Airport_view_from_landing_strip.jpg"];
    UIImage *chareleroiimage = [UIImage imageNamed:@"Aéroport_de_Charleroi,_B737_Ryanair_-_2011.JPG"];
    UIImage *liegeimage = [UIImage imageNamed:@"Liege_airport.jpg"];
    UIImage *oostendeimage = [UIImage imageNamed:@"Luchthavengebouw_Antwerpen-Deurne.jpg"];

    switch (row) {
        case 0:
            characterimage.image = bergeracimage;
            break;
        case 1:
            characterimage.image = biarritizimage;
            break;
        case 2:
            characterimage.image = chareleroiimage;
            break;
        case 3:
            characterimage.image = liegeimage;
            break;
        case 4:
            characterimage.image = oostendeimage;
            break;
        default:
            characterimage.image = oostendeimage;
            break;
    }
}

How would I implement the UIButton?

I have been trying to incorporate it in each case branch, but so far no luck.

Edit:

Ok i have been messing around with it for the pass hour or so and now the code looks like this as im trying the change the link within the UIbutton;

{
    //Create UIImage
    UIImage *bergeracimage = [UIImage imageNamed:@"Bergerac.JPG"];
    UIImage *biarritizimage = [UIImage imageNamed:@"Biarritz-Anglet-Bayonne_Airport_view_from_landing_strip.jpg"];
    UIImage *chareleroiimage = [UIImage imageNamed:@"Aéroport_de_Charleroi,_B737_Ryanair_-_2011.JPG"];
    UIImage *liegeimage = [UIImage imageNamed:@"Liege_airport.jpg"];
    UIImage *oostendeimage = [UIImage imageNamed:@"Luchthavengebouw_Antwerpen-Deurne.jpg"];

    //Create UIButton
    UIButton *bergeraclink = [NSURL URLWithString:@"https://www.google.com"];

    switch (row) {
        case 0:
            characterimage.image = bergeracimage;
            _characterpath = bergeraclink;
        case 1:
            characterimage.image = biarritizimage;
            break;
        case 2:
            characterimage.image = chareleroiimage;
            break;
        case 3:
            characterimage.image = liegeimage;
            break;
        case 4:
            characterimage.image = oostendeimage;
            break;
        default:
            characterimage.image = oostendeimage;
            break;
    }
}

It still doesnt work but there are no errors in the code etc and the app runs, am i going about this the wrong way?

1

1 Answers

0
votes

Sounds like you need a property (or ivar) to hold the correct URL to call. Then you'll have access to the URL when your button is tapped. Maybe something like this:

//a property to hold the URL
@property (strong, nonatomic) NSString *urlString;

...

//set the URL in your switch statement
switch (row) {
        case 0:
            characterimage.image = bergeracimage;
            self.urlString = @"http://yourserver.com/bergerac";
            break;
        ...

-(IBAction)buttonTapped {
    //call your web service with self.urlString 
}