0
votes

Im working on a Countries and Capitals app as part of an Objective-C Course.

So far when the user selects a Country in the picker the relevent Capital City is displayed in the label below.

My question is how can I code it so the Capital City is displayed only after the user clicks the "Get Capital" button.

I know this seems less efficient but it would be useful to know how!

SIMULATOR PIC

countryNames =[[NSArray alloc]initWithObjects:@"England",
                                              @"United States",
                                              @"India",
                                              @"Australia",
                                              @"Canada",
                                              @"France",
                                              @"The Netherlands",
                                              @"South Africa",nil];
cityNames =[[NSArray alloc]initWithObjects:@"London",
                                           @"Washington DC",
                                           @"Dehli",
                                           @"Canberra",
                                           @"Ottawa",
                                           @"Paris",
                                           @"Amsterdam",
                                           @"Cape Town",nil];

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

lblcapitalCity.text =[cityNames objectAtIndex:row];

}

// Not sure after doing this how to only display cities after a button is clicked

1
Don't do anything in didSelectRow. Create a button and attach an action to it that assigns lblCapitalCity.text based on [pickerView selectedRowInComponent:0]David Berry
Thanks @David but I still don't understand after reading your solution. What changes should I make to: -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component and what should I code here: - (IBAction)getCapitalButton:(id)sender {}George_UK

1 Answers

0
votes

Eliminate your pickerView:didSelectRow:inComponent: completely and use this action for the button:

-(IBAction)getCapitalButton:(id)sender
{
    lblCapitalCity.text = cityNames[[pickerView selectedRowInComponent:0]];
}