【问题标题】:UIViewController subclass strategyUIViewController 子类策略
【发布时间】:2013-02-16 02:51:46
【问题描述】:

假设我有一个选项卡式应用程序,其中每个选项卡显示不同类别的相似数据(例如,Tab1:“猫图片”,Tab2:“狗图片”,Tab3:“马图片”等...)。

每个选项卡的视图控制器都以相同的方式呈现数据。唯一的区别是 vc 的 title 属性和获取数据的方法的参数。那么以下选项的优缺点是什么:

  • 创建一个具有(有限)分支的 ViewController 主控 每个类别的逻辑。

  • 创建一个 ViewController 不同类别类型的超类和几个子类。

编辑

所以为了澄清“获取数据的方法的参数”:获取方法可能如下所示:

-(void)fetchDataForType:(NSString*)type {
       if ([type isEqual:@"cat"])
           // fetch cat pics
       if ([type isEqual:@"dog"])
           // fetch dog pics
       ...
 }

【问题讨论】:

  • 请澄清“以及获取数据的方法的参数”。这些区别是什么?

标签: ios objective-c cocoa-touch uiviewcontroller uikit


【解决方案1】:

在你的情况下,我什至不会继承。创建一个具有您可以更改的属性的类,并使用不同的属性值进行实例化。

粗略的例子:

@interface ViewController : UIViewController
@property (retain, nonatomic) NSString *title;
- (void)getJSONWithParams:(NSDictionary *)params;
@end

实例:

ViewController *controller1 = [[ViewController alloc] init];
controller1.title = @"First";
NSDictionary *params1 = @{@"url":@"http://graph.facebook.com", @"query":@"q=somethingInFacebook"};
[self.navigationController pushViewController:controller1];
[controller1 getJSONWithParams:params1];

ViewController *controller2 = [[ViewController alloc] init];
controller2.title = @"Second";
NSDictionary *params2 = @{@"url":@"http://api.twitter,com", @"query":@"q=somethingInTwitter"};
[self.navigationController pushViewController:controller2];
[controller2 getJSONWithParams:params2];    

【讨论】:

    【解决方案2】:

    短期项目:

    • 我会保留一个UIViewController,用于更改数据源(在这种情况下是图片的类型)

    长期项目:

    • 我可能会创建子类,因为您可能需要在之后添加较小的差异,现在创建子类可以为您节省一些时间。即使在短期项目中,您也可以使用这种方法。由于数据源是唯一发生变化的,因此子类大部分为空。

    【讨论】:

      【解决方案3】:

      Create AbstractViewController 包含所有公共信息并具有所有方法。根据需要创建多个ViewControllers,它们继承自super,并用它们的行为覆盖函数。

      【讨论】:

        【解决方案4】:

        我相信单个 UIViewController 子类可以提供您需要的一切。您只需要提供支持每种不同类型图片所需的差异。让我们假设这些差异只是它显示的图片的描述和类型:

        MyViewController.h:

        @MyViewController : UIViewController
        
        @property (strong, nonatomic) NSString *description;
        @property (strong, nomatomic) NSString *type;
        
        // Designated initializer
        - (id)initWithNibName:(NSString *)nibName
                       bundle:(NSBundle *)nibBundle
                  description:(NSString *)description
                         type:(NSString *)type;
        
        @end
        

        MyViewController.m:

        @implementation MyViewController
        
         - (id)initWithNibName:(NSString *)nibName
                        bundle:(NSBundle *)nibBundle
                   description:(NSString *)description
                          type:(NSString *)type
        {
            self = [super initWithNibName:nibName bundle:nibBundle];
            if (self != nil)
            {
                self.description = description;
                self.type = type;
            }
            return self;
        }
        
        - (NSArray *)fetchPics
        {
            NSMutableArray *pics = [[NSMutableArray alloc] init];
        
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.somewhere.com/fetchPics?type=%@", self.type]];
            ...
        
            return pics;
        }
        
        @end
        

        然后它应该只是在必要时创建视图控制器的情况:

        MyViewController *catVC = [[MyViewController alloc] initWithNibNamed:@"Something" bundle:nil description:@"Cats" type:@"cat"];
        ...
        

        【讨论】:

          【解决方案5】:

          我根本不会子类化。您创建一个视图控制器,其属性可以在设置时更改,并为每个选项卡创建一个实例。没有什么可以阻止您创建视图控制器的多个实例。一个视图层次结构不需要一个视图控制器。

          【讨论】:

            猜你喜欢
            • 2016-07-28
            • 2015-05-08
            • 1970-01-01
            • 2016-01-28
            • 1970-01-01
            • 1970-01-01
            • 2020-09-17
            • 2017-01-05
            • 1970-01-01
            相关资源
            最近更新 更多