1

我有一个文本字段。一旦我点击它,我想要一个UIPickerView弹出并从中选择一个项目,UIPickerView一旦我完成选择,我想通过点击一个上面UIActionSheet有确认按钮来确认选择。所以我的要求是有一个UIActionSheet在其下方是一个选择器视图。我只是 iOS 开发的初学者,还不熟悉技术术语。所以请容忍我的非正式提问方式。

这是我的代码块:

创建 UIPickerView 和 UIActionSheet

UIActionSheet *confirmRoomSelectionAS =[[UIActionSheet alloc]initWithTitle:@"" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Done",nil];

UIPickerView *roomTypePicker=[[UIPickerView alloc]init];

[confirmRoomSelectionAS addSubview:roomTypePicker];
[confirmRoomSelectionAS showInView:self.view];
[confirmRoomSelectionAS setBounds:CGRectMake(0, 0, 320, 600)];

[roomTypePicker setFrame:CGRectMake(0, 170, 320, 216)];
[roomTypePicker setDataSource:self];
[roomTypePicker setDelegate: self];
roomTypePicker.showsSelectionIndicator = YES;
4

2 回答 2

2

像这样试试

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[actionSheet showInView:[self.view window]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 390)];
[actionSheet addSubview:[self createToolbar:@"Test"]];

UIPickerView *picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 320, 162);
picker.showsSelectionIndicator = YES;
picker.dataSource = self;
picker.delegate = self;
[actionSheet addSubview:picker];

- (UIView *)createToolbar:(NSString *)titleString {

    UIToolbar *inputAccessoryView = [[UIToolbar alloc] init];
    inputAccessoryView.barStyle = UIBarStyleBlackOpaque;
    inputAccessoryView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [inputAccessoryView sizeToFit];
    CGRect frame = inputAccessoryView.frame;
    frame.size.height = 44.0f;
    inputAccessoryView.frame = frame;

    UIBarButtonItem *doneBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
    UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *cancelBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0 , 11.0f, 100, 21.0f)];
    [titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:14]];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    [titleLabel setTextColor:[UIColor whiteColor]];
    [titleLabel setText:titleString];
    [titleLabel setTextAlignment:UITextAlignmentCenter];

    UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];


    NSMutableArray *array = [NSMutableArray arrayWithObjects:cancelBtn,flexibleSpaceLeft,title,flexibleSpaceLeft, doneBtn, nil];
    [inputAccessoryView setItems:array];

    [doneBtn release];
    [title release];
    [titleLabel release];
    [flexibleSpaceLeft release];
    [cancelBtn release];

    return [inputAccessoryView autorelease];
}

- (void)done:(id)sender {
}

- (void)cancel:(id)sender {
}

它会像,

在此处输入图像描述

于 2013-05-04T12:43:48.697 回答
0

我认为你应该修改的 3 件事:

1)手动添加取消按钮,以便以后更好地检查取消按钮按下事件

UIActionSheet * confirmRoomSelectionAS = [[UIActionSheet alloc] 
                         initWithTitle:@"" 
                         delegate:self 
                         cancelButtonTitle:nil 
                         destructiveButtonTitle:nil 
                         otherButtonTitles:nil];
[confirmRoomSelectionAS addButtonWithTitle:@"Done"]; 
confirmRoomSelectionAS.cancelButtonIndex = [confirmRoomSelectionAS addButtonWithTitle: @"Cancel"];
[confirmRoomSelectionAS showInView:self.view];

2)实现你的动作表委托方法

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == actionSheet.cancelButtonIndex) {
    // cancel button pressed;
        return;
    }
}

3)您应该在 .h 中声明 *UIPickerView选择器,以便稍后通过添加如下内容检查其在前一个委托方法(也包括完成检查)中的选定值:

NSInteger row;
row = [picker selectedRowInComponent:0];
于 2013-05-04T23:50:32.420 回答