显示选择器视图和操作表

时间:2013-05-04 11:30:51

标签: iphone ios objective-c xcode uipickerview

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

这是我的代码块:

创建UIPickerView和UIActionSheet

UIActionSheet

2 个答案:

答案 0 :(得分: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 {
}

就像,

enter image description here

答案 1 :(得分: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 选择器,以便稍后通过添加这样的sthing来检查上一个委托方法中的选定值(也包括完成检查):

NSInteger row;
row = [picker selectedRowInComponent:0];