简体   繁体   中英

ios, code-reuse issue, reuse viewController?

I have a view controller(A) which shows a list of items download from server.

I have another view controller(B) with segmented control.
When second index of segmentedControl is selected, the view controller(B) shows a list of items in the same format as in the controller(A).
When first index of segmentedControl is selected, the view controller(B) shows the items in different format.

I want A and B share a code for the common stuff.

I first thought ok I could move all the view-related code to "common_view class" and use the view class from both controllers.
But, it turns out that there are codes which are not related to view(such as downloading stuff from server, which is controller-part of mvc pattern).

Now I'm perplexed, "is it a good idea to share a controller-part code? is it even possible?"

I could factor out a common code into a commonViewController, and instantiate it from controller(A) and controller(B), and add commonViewController.view as a subview.
But is this really desirable? or do you suggest any better way to do this?

是的,绝对可以。在这种情况下,您可以使用两个NIBS。第一个NIB将由ViewController A和ViewController B的第一段使用。在ViewController B中选择第二段时,请添加viewControllerA的subSubView并使用initsWithSecondNiB名称。 ,当在viewcontrollerB的两个部分之间切换时,通过显示/隐藏来控制其可见性

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM