Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the selected text field in iOS

I am developing one iPad application using story board.In my application i have popup view with two text fields and one button.what i need to do means if i click the button one text is need to display in the selected text box. How i find which text box is selected at the present situation? Example code is give below Please help me to complete function.

- (IBAction)buttonPressed:(id)sender {
     if(//Here i need to set the condition for check selected text box is first){
         texbox1.text=@"User";
      }
      else{
         textbox2.text =@"Admin";
      }
}
like image 446
user3762713 Avatar asked Dec 03 '25 01:12

user3762713


2 Answers

- (IBAction)buttonPressed:(id)sender {

   if([textbox1 isFirstResponder]){
       //Cursor on textbox1
       texbox1.text=@"User";
   }
   else if([textbox2 isFirstResponder]){
      //Cursor on textbox2
       textbox2.text =@"Admin";
   }
   else{

       NSLog(@"None of them have cursor");
   }
}
like image 84
Vijay-Apple-Dev.blogspot.com Avatar answered Dec 05 '25 15:12

Vijay-Apple-Dev.blogspot.com


  1. Set Tag to your textfields

  2. Implement UITextFieldDelegate in your class

  3. Use this methods

    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        if (textField.tag == your tag) {
            selectedTextField = textField; //selectedTextField is an UITextfield variable
        }
    }
    
  4. Use it in your IBAction

    - (IBAction)buttonPressed:(UIButton *)sender {
        selectedTextField.text = @"abc";
    }
    

//Edit: something wrong with StackOverFlow's code function, doesn't display properly

like image 28
Pham Hoan Avatar answered Dec 05 '25 15:12

Pham Hoan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!