2011-02-18 66 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。