2014-02-16 132 views
0

所以我有一個開關,當它「開」我想CPPickerView切換到一個數組中的特定值。另外,如果pickerview再次移動,我希望開關移動到關閉位置。以編程方式更改CPPickerview選擇?

我知道如何獲取當週的當天,並試圖將pickerview選項切換到當前的星期幾。

如果我在這裏問一個這樣的廣義問題只是讓我知道,或者如果你需要更多的信息。

//CPPicker 
    self.daysOfWeekData = [[NSArray alloc] initWithObjects:@"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", @"Sunday", nil]; 
    self.dayPickerView.allowSlowDeceleration = YES; 
    [self.dayPickerView reloadData]; 
#pragma mark - Horizontal pickerview 

//DataSource 
-(NSInteger)numberOfItemsInPickerView:(CPPickerView *)pickerView { 
    return 7; 
} 

-(NSString *)pickerView:(CPPickerView *)pickerView titleForItem:(NSInteger)item { 
    return daysOfWeekData[item]; 
} 

//Delegate 
-(void)pickerView:(CPPickerView *)pickerView didSelectItem:(NSInteger)item { 
    self.dayLabel.text = [NSString stringWithFormat:@"%@", daysOfWeekData[item]]; 
} 

//Today's day date 
- (IBAction)todaySwitchChange:(id)sender { 

    if (todaySwitch.on) { 

     NSLog(@"It is on"); 

    } else { 

     NSLog(@"It is off"); 

    } 
} 

回答

0

這可以通過使用CPPickerView的setSelectedItem:animated:方法以及正常的委託方法來完成。

在當開關接通時,CPPickerView設爲您想要的索引你todaySwitchChange:方法:

//Today's day date 
- (IBAction)todaySwitchChange:(id)sender { 

    if (todaySwitch.on) { 
     NSLog(@"It is on"); 

     // This will cause the CPPickerView to select the item you choose 
     NSUInteger itemToSelect = someValue; //whatever logic you need to select the right index 
     [self.dayPickerView setSelectedItem:itemToSelect animated:YES]; // Or disable animation if desired 

    } else { 

     NSLog(@"It is off"); 

    } 
} 

要切換開關斷開時,在CPPickerView用戶滾動,你需要掛接到委託方法,讓你通知已發生滾動:

// Implement the following delegate method 
- (void)pickerViewWillBeginChangingItem:(CPPickerView *)pickerView { 
    // Picker is going to change due to user scrolling, so turn the switch off 
    if (todaySwitch.on) { 
     todaySwitch.on = NO; 
    } 
} 

希望這有助於!

相關問題