UIPickerView和UITextFields问题

时间:2012-06-03 13:11:31

标签: objective-c uitextfield uipickerview

我有多个textFields。在其中一个中,我想显示UIPickerView。 我是这样做的:

-(void)showPicker{
    [subject becomeFirstResponder];
    actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                             delegate:nil
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

    picker = [[UIPickerView alloc] initWithFrame:pickerFrame];
    picker.showsSelectionIndicator = YES;
    picker.dataSource = self;
    picker.delegate = self;

    [actionSheet addSubview:picker];
    [picker release];

    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems: [NSArray arrayWithObject:@"Close"]];
    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:[[UIApplication sharedApplication] keyWindow]];

    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];



}

-(void)dismissActionSheet{
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

当我关闭它时,工作表正在被解除,但subject textField上的键盘出现了。

现在:

如何才能在subject textField上显示键盘?

这是我辞职的地方:

- (void) resignKeyboard:(id)sender{

        if ([name isFirstResponder])
            [name resignFirstResponder];
        else if ([email isFirstResponder])
            [email resignFirstResponder];
        else if ([message isFirstResponder])
            [message resignFirstResponder];
        else if ([subject isFirstResponder]) {
            [subject resignFirstResponder];
        }

}

3 个答案:

答案 0 :(得分:0)

可以防止键盘弹出。

首先将您的类设置为UITexfield的委托

 textField.delegate = self

在您的界面声明中添加:

<UITextFieldDelegate> 

现在实现textFieldShouldBeginEditing ::

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // Show UIPickerView

    return NO;
}

答案 1 :(得分:0)

使用UItextField委托:为此你需要添加@interface YourViewController:UIViewController

如果通过右键单击从xib获取的UItextField将其委托添加到fileowner

现在在代码中使用此方法:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
   if(textField == subject)
   {
     return NO;
   }
   return YES;
}

答案 2 :(得分:0)

使用透明按钮解决,该按钮处理显示拾取器的操作。 “subject”textField已被禁用。