Public vs. Private IBOutlets

Interestingly, what is considered best practice in a situation where I need to read the contents of a controller UILabel/ UITextFieldfrom another between the two following possibilities:

1) Just create IBOutletin the UITextField.h file and make it publicly available for everyone
2) Create the NSString readonly property in the .h file, make it readwrite in the implementation file and update it every time you change private UITextField .

In my opinion, the second option looks better, because it supports OOP encapsulation, but there seems to be a lot of work with each UITextField / UILabelI have in each view controller.

Thoughts? Thank!

EDIT: Also, if I need to be able to install IBOutletsexternally as well in a method -(void)prepareForSegue:. So, I think the second option is the only option?

+4
source share
1 answer

Just create getter methods in the view controller to return the required data. For example, for UITextField:

- (NSString *)getTextFieldText {
   return self.myTextField.text;
}

There is no need to add a redundant property (NSString *) in this way. As with encapsulation, you hide personal data (i.e. your IBOutlets) and expose only the data that other users require.

+2
source

Source: https://habr.com/ru/post/1542000/


All Articles