2011-02-18 58 views
0

我創建了一個UIPickerView,它顯示在按鈕單擊上。問題是我如何填寫行項目iPhone填充UIPickerView的值

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Location" 
                  delegate:self 
                cancelButtonTitle:@"Cancel" 
               destructiveButtonTitle:nil 
                otherButtonTitles:nil]; 

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent]; 
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100,0,0)]; 
    pickerView.showsSelectionIndicator = YES; 
    pickerView.dataSource = self; 
    pickerView.delegate = self; 
    [actionSheet addSubview:pickerView]; 
    [pickerView release]; 
    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(@"Done", @"")]]; 
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f); 
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar; 
    closeButton.tintColor = [UIColor blackColor]; 
    [closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged]; 
    [actionSheet addSubview:closeButton]; 
    [closeButton release]; 
    [actionSheet showInView:self.view]; 
    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)]; 

和點擊「完成」時如何獲得值。

+0

而不是搞亂管理actionSheet,pickerView等,我會建議使用[EAActionSheetPicker](https://github.com/EckyZero/EAActionSheetPickerDemo)。它真的清理你的代碼很多。 – eckyzero 2013-05-22 17:23:12

回答

1

你已經設置了警報的委託和選擇器。現在你必須執行它們。您不填寫選擇器的數據,選擇器會向您的代表請求數據。

您需要在這三個協議中定義的強制功能。

UIPickerViewDelegate
UIPickerViewDataSource
UIActionSheetDelegate

您必須實現-(IBAction)dismissActionSheet:(UIButton *)sender了。

以及點擊「完成」後我如何獲得價值。

每次選擇器發生變化時,我都會將值存儲到實例變量中,一旦完成按鈕被單擊,我會將其分配給我的數據模型。


編輯:如果您想了解的東西我會擺脫複製&糊液,並嘗試實施它在我自己的。但是YMMV。