0

I'm trying to create an unit converter app and not sure which command to implement for proceeding further. Below code would return to main screen upon hitting return key in number pad or tapping anywhere on the screen.

- (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];
}

And This code would open a picker view:

- (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;
}

How could I open a picker view upon hitting return key or tapping anywhere on the screen? Kindly assist.

2 Answers2

1

1- You can use Tap gesture recogniser initWithTarget:self action:@selector(handleTapFrom:)];

2- Use textfield delegate -(BOOL) textFieldShouldReturn { return true; // do stuff }

Abdul Rehman
  • 613
  • 6
  • 9
0

You can add UITapGestureRecognizer on you main view like this. And can utilize UITextFieldDelegate to check for return button pressed.

- (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;
}
Afnan
  • 888
  • 1
  • 13
  • 37
  • did you mean delete button? I guess there is no return button on numpad. – Afnan Aug 13 '14 at 17:36
  • Thank You. This works fine while tapping anywhere on the screen but not on hitting return key. Also, hide button which was previously working is not working now. Below is the hide pickerview code: -(IBAction)hideButton:(id)sender { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3]; pickerViewContainer.frame=CGRectMake(0, 460, 320, 261); [UIView commitAnimations]; } – user2855057 Aug 13 '14 at 17:37
  • I have the return button on the right most corner on the ios numpad. It is something similar to done button – user2855057 Aug 13 '14 at 17:41
  • Can you add a screenshot for it? – Afnan Aug 13 '14 at 17:43
  • Im not able to post screenshot due to error "Need atleast 10 reputation to post an image" on this website. However, mine looks something similar to this - "http://i.stack.imgur.com/bvUwo.png" Instead of "Done" button, I have "Return" button – user2855057 Aug 13 '14 at 17:54
  • Ok. I have made the return key to disappear. What I wanted was to open picker view once the return key disappears. Your solution works like charm for tapping anywhere on the screen. I have made changes to the UITextFieldDelegate code. Below is the code. - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [self.distance resignFirstResponder]; [self openPickerView]; return YES; } I need the code to execute resignFirstResponder first and openPickerView next. But it executes either of the one but not both. Is there a way to execute both the lines parallely or one after another? – user2855057 Aug 13 '14 at 19:33
  • You can add delay like this. - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [self.distance resignFirstResponder]; double delayInSeconds = 2.0; //delay you want dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [self openPickerView]; }); return YES; } Also please accept answer if you find correct. or vote up if it is helpful. – Afnan Aug 13 '14 at 19:52