开发者

how to link text field data between two views?

开发者 https://www.devze.com 2023-02-20 11:19 出处:网络
Hi and thanks in advance for any responses. I\'m working with two UIViews with the Ipad. If the user makes changes to the text field in one view, I want those changes to appear in the textfield of th

Hi and thanks in advance for any responses.

I'm working with two UIViews with the Ipad. If the user makes changes to the text field in one view, I want those changes to appear in the textfield of the other view开发者_开发技巧. I have one text field in each view. In IB, I gave them the same tag and textfield delegate. I only have one IBOutlet but I dont think it possible to connect to both text field? I tried IBOutletCollection but it doesn't support textfields. I don't know where to go from here?What should I do? Any advice is appreciated!thanks!


The answer is bindings an KVO(Key-Value-Observation). The idea behind this is: One object observes changes of another object's property.

Have a look at KVO and Bindings on iPhone on Mindsizzlers.com or take a look at the book Cocoa Design Patterns Chapter 32 Bindings and Controllers.


You can use the UITextField delegate methods to detect text changes in one text field and update the other. The method below gets called every time a character is typed in one of the textfields (provided you have set your view controller to be the UITextField delegate):

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // Here you can run your logic to detect which textField has changed (using tags)
    // And update them accordingly.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号