Reputation: 884
I wonder what is considered to be a better practice in a situation when I need to read the contents of the UILabel
/ UITextField
from another view controller between the following two possibilities:
1) Simply create an IBOutlet
to the UITextField
in the .h file and make it public to everyone
2) Create an NSString
readonly property in the .h file, make it readwrite in the implementation file, and update it every time the private UITextField
changes.
In my opinion, the second option looks a better approach, because it maintains the OOP encapsulation, but it seems a lot of work to do with every UITextField / UILabel
I have in every view controller.
Thoughts? Thanks!
EDIT: In addition, if I need to have the ability to set the IBOutlets
from outside, but in a -(void)prepareForSegue:
method. So I guess the second option is the only option?
Upvotes: 4
Views: 480
Reputation: 13713
Just create getter methods in the view controller for returning the required data. For example for UITextField
:
- (NSString *)getTextFieldText {
return self.myTextField.text;
}
No need to add a redundant property (NSString *) this way. As with encapsulation you are hiding the private data (i.e your IBOutlets) and expose only the data that is required by others
Upvotes: 2