Domanda

I'm using RubyMotion (had been writing Obj C in Xcode previously but now I'm collaborating with someone that wants to use RubyMotion due to bkgd in Ruby)

I have a ViewController with 2 textfields. I need to have it so that whenever user switches from being in textfield1 to textfield2 if a condition of textfield1 isn't met, Error Label shows (I use self.ErrorLabel.show for this). I know how to write my condition statements but I really do not know how to know when user switches to next textfield.

I thought I could use:

if ([textField1 isFirstResponder] && (textField1 != touch.view))
     log( 'left textfield1' )
end

if ([textField2 isFirstResponder] && (textField2 != touch.view))
     log( 'left textfield2' )

end

from this question Detect UITextField Lost Focus but no such luck. I know I'm using RubyMotion so there are some differences.

How do I get this to work? I don't think I'm searching with the right keywords because it seems like something developers use all the time yet I'm not finding any results.

Thank you for any help available.

Update: I am thinking I will be using this method:

- (void)textFieldDidEndEditing:(UITextField *)textField

In RubyMotion: def textFieldDidEndEditing( textField )

I used log() to determine that this does indeed let me know that the user has changed from one textfield to the next. I just need to do a couple of tweaks so that I can specify which textfield was left.

È stato utile?

Soluzione

You need to set the delegate of each of your text fields to the view controller class, then implement the textFieldDidEndEditing method. Here's a working example:

class MyViewController < UIViewController
  attr_accessor :textField1, :textField2

  def viewDidLoad

    @textField1 = UITextField.alloc.initWithFrame([[10, 100], [300, 40]])
    # Customize your text field...
    @textField2 = UITextField.alloc.initWithFrame([[10, 150], [300, 40]])
    # Customize your text field...
    view.addSubview(@textField1)
    view.addSubview(@textField2)

    @textField1.delegate = self
    @textField2.delegate = self
  end

  def textFieldDidEndEditing(textField)
    if textField == @textField1
      puts 'left textField1'
    elsif textField == @textField2
      puts 'left textField2'
    end
  end
end
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top