0

我正在尝试创建一个单位转换器应用程序,但不确定要实施哪个命令以进一步进行。下面的代码将在按数字键盘中的返回键或点击屏幕上的任意位置时返回主屏幕。

- (IBAction)textFieldReturn:(id)sender
{
    [sender resignFirstResponder];    
    [self selectButton:nil];
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   UITouch* touch= [[event allTouches]anyObject];    
    if ([_distance isFirstResponder] && [touch view] != _distance)
    {
        [_distance resignFirstResponder];
    }

    [super touchesBegan:touches withEvent:event];
}

这段代码将打开一个选择器视图:

- (IBAction)selectButton:(id)sender
{
  [UIView beginAnimations:nil context:nil];
  [UIView setAnimationDuration:0.3];
   pickerViewContainer.frame=CGRectMake(0, 200, 320, 261);
  [UIView commitAnimations];
   pickerViewContainer.hidden=NO;
}

按回车键或点击屏幕上的任意位置时,如何打开选择器视图?请协助。

4

2 回答 2

1

1-您可以使用点击手势识别器initWithTarget:self action:@selector(handleTapFrom:)];

2-使用文本字段委托-(BOOL) textFieldShouldReturn { return true; // do stuff }

于 2014-08-15T04:55:25.297 回答
0

您可以UITapGestureRecognizer 像这样添加主视图。并且可以UITextFieldDelegate用来检查是否按下了返回按钮。

- (void) viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openPickerView)];
    [self.view addGestureRecognizer:gestureRecognizer];
    [self.textField setDelegate:self];
}

//UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self openPickerView];
    return YES;
}


- (void)openPickerView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    pickerViewContainer.frame=CGRectMake(0, 200, 320, 261);
    [UIView commitAnimations];
    pickerViewContainer.hidden=NO;
}
于 2014-08-13T10:25:34.300 回答