2012-10-25 28 views
2

我使用的this code「模擬」下拉菜單,因爲它在html代碼中工作。我在頁面上添加了2個下拉菜單。當我在第一個下拉菜單中選擇一個項目時,需要通知我的控制器,並且需要在此通知中提供選定下拉項目中的選定ID。如何在第一個下拉列表中選擇一個項目時通知我的控制器?還有,我如何在第一個下拉列表中打包所選標識的通知?如何通知控制器需要更新

我初始化用下面的代碼下拉:

ddDuration = [[UIDropDownMenu alloc] init]; 
[ddDuration makeMenu:txtDurationId titleArray:arrDurationIds valueArray: arrDurationNames targetView:self.view]; 
[ddDuration setDropdownTextColor:[UIColor whiteColor]]; 
[ddDuration setDropdownBackgroundColor:[UIColor darkGrayColor]]; 

arrDurationsIdarrDurationNames包含相同數目的元素。第一個包含ID和後者的名稱。

然後,我希望下面的方法在dropdown 1中被選中時調用。發件人需要包含來自dropdown 1的選定項目ID - 以便dropdown 2知道應將哪些內容加載到該項目中。

- (IBAction)didSelectCountry:(id)sender { 
    [ddDestination makeMenu:txtDestinationId titleArray:arrDepartureIds valueArray: arrDepartureNames targetView:self.view]; 
    [ddDestination setDropdownTextColor:[UIColor whiteColor]]; 
    [ddDestination setDropdownBackgroundColor:[UIColor darkGrayColor]]; 
} 
+0

所以基本上你想在被選擇的對象,並從菜單就知道..? –

回答

0

您的解決方案與您下載代碼的位置相同。勾選此

[menu1 addObserver:self forKeyPath:@"selectedValue" options:NSKeyValueObservingOptionNew context:@"menu1"]; 

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change (NSDictionary *)change context:(NSString *)context 
{ 
    // get the selected menu and value 
    NSString* changedObject = context; 
    NSString* changedValue = [change objectForKey:NSKeyValueChangeNewKey]; 

    NSLog(@"%@ has changed to %@", changedObject, changedValue); 

    // Menu 1 selected 
    if ([context isEqualToString:@"menu1"]){ 
     // create a new array for menu 2. 
     NSArray *arrayNames2 = [[NSArray alloc] initWithObjects: 
         @"Erik Vanderwal", 
         @"Max Town", 
         @"Avis Villalon", 
         @"Hugh Salvia", 
         nil]; 

     // rebuild menu 2 with the new array values 
     [menu2 makeMenu:textfield2 titleArray:arrayNames2 valueArray: arrayNames2 targetView:self.view]; 

     // run makemenu with nil array values to clear the old values from menu 3 
     [menu3 makeMenu:textfield3 titleArray:nil valueArray: nil targetView:self.view]; 

    } 

    // Menu 2 selected 
    if ([context isEqualToString:@"menu2"]){ 
     NSArray *arrayNames3 = [[NSArray alloc] initWithObjects: 
          @"Erik Vanderwal", 
          @"Hugh Salvia", 
          nil]; 

     [menu3 makeMenu:textfield3 titleArray:arrayNames3 valueArray: arrayNames3 targetView:self.view]; 

    } 


} 
相關問題