Domanda

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

È stato utile?

Soluzione

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

-(IBAction)getCapitalButton:(id)sender
{
    lblCapitalCity.text = cityNames[[pickerView selectedRowInComponent:0]];
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top