0

How can i trigger done button touch in iPhone?

I have an asynchronous task which pops me small tableView (adds it to subView). When one of my textFields is focused - keyboard is shown and overlays my tableView.

I tried to call resignFirstResponder on focused textField in "textFieldShouldBeginEditing" and it didn't help. So i think maybe explicit trigger of done button will work.

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
Misha
  • 5,260
  • 6
  • 35
  • 63
  • Triggering the the button press is never necessary, it doesn't do anything that you can't do yourself in code. Also, you question is a little bit unclear to me (might just be me, sorry). Are you trying to resign the keyboard or are you trying to move the keyboard so that it is not in the way of the tableView? – sosborn Jul 13 '11 at 04:26
  • I'm trying to resign the keyboard. – Misha Jul 13 '11 at 05:56
  • http://stackoverflow.com/questions/274319/how-do-you-dismiss-the-keyboard-when-editing-a-uitextfield – sosborn Jul 13 '11 at 06:21

4 Answers4

0

You can animate the entire frame of the view whenever the textfield is focused, so that it slides up and display the table and also the textfield without any hindrance.

stack2012
  • 2,146
  • 2
  • 16
  • 23
0

you could have the done button as right navigation button of your view controller.

  UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonPressed:)];          
  self.navigationItem.rightBarButtonItem = doneButton ;
  [doneButton release];

Implement doneButtonPressed: method as below.

-(void) doneButtonPressed:(id) sender
{
   [myTextField resignFirstResponder];
}
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • I have lots of textFields. How do i know which one is active when doneButtonPressed called. By the way i saved the textField when entered textFieldShouldBeginEditing method. And when my tableView poped up i called resignFirstResponder for this field and it didn't work. – Misha Jul 13 '11 at 06:00
0

If you don't want the text field to becomeFirstResponder or to be edited, return NO from textFieldShouldBeginEditing method.

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
0

What I understood from your question that your other fields get hided when you enter in the textfield.For this you need to animate the view.I am going to put the code for this for you.

In your header file declare this.

CGFloat animatedDistance;

And in your .m file put this code and you won't face any problem.Your view will automatically scroll up and down when you start editing the text fields so making the space to view your fields properly..

#pragma mark -
#pragma mark (Text Field Delegate Method)
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


-(void)textFieldDidBeginEditing:(UITextField *)textField
{

    static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
    static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
    static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
    static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
    static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;

    CGRect textFieldRect;
    CGRect viewRect;

    textFieldRect =[addProScrollView.window convertRect:textField.bounds fromView:textField];
    viewRect =[addProScrollView.window convertRect:addProScrollView.bounds fromView:addProScrollView];


    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;

    if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }

    UIInterfaceOrientation orientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait ||orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }

    CGRect viewFrame;

    viewFrame= addProScrollView.frame;
    viewFrame.origin.y -= animatedDistance;


    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [addProScrollView setFrame:viewFrame];

    [UIView commitAnimations];

}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if(textField.tag==0)
    {
        static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
        CGRect viewFrame;

        viewFrame= addProScrollView.frame;
        viewFrame.origin.y += animatedDistance;

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];


        [addProScrollView setFrame:viewFrame];
        [UIView commitAnimations];



    }
}

Thats it .You are now done just run your code.

Cheers......

Sabby
  • 2,586
  • 2
  • 27
  • 41