开发者

One IBOutlet for two UITextFields

I have two UITextField one for Portrait and ot开发者_如何学编程her for Lanscape View but I want that editing the first should reflect the changes to other text field too. I have to use separate views for both landscape and portrait. My last option is to make two outlets for them but I want to use only a single one..

Any help would be appreciated.


One option is to make use of IBOutletCollection. You can have one NSArray which you declare as an IBOutletCollection (instead of IBOutlet). In order to know what text field you are getting out of the array, you can set a tag on each one in IB and just pull from the array the text fields that match specific tags.

There is another workaround. You store the values from the text fields before you use the nib for the changed orientation like

NSString *firstText = firstTextField.text;
NSString *secondText = secondTextField.text;

then after exchanging the nibs use this to populate the text fields in the new nib like

firstTextField.text = firstText;
secondTextField.text = secondText;


You might implement appropriate method(s) in UITextFieldDelegate.....

Such as

//not real code here just off the top of my head
(BOOL)textFieldShouldEndEditing:(UITextField *)textField {

    NSString* updatedText = [textField text];

    firstTextField.text = updatedText;
    secondTextField.text = updatedText;

    return YES;
}


u can use 2 diiferent xib files and do the same thing To load a new xib file use the following code [[NSBundle mainBundle] loadNibNamed:@"tests" owner:self options:nil]; u can still use the existing xib file by duplicating it and and loading it with the above code when the orientation changes. There wont be much changes in the code as u can use the same iboutlets in both the orientations without even requiring to manually link them

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜