如何在多个viewController中重用组件?

时间:2014-03-29 19:26:38

标签: ios objective-c

我一直在经历一个IOS项目,我在其中找到了多个viewController中使用的相同代码集。

示例:

xxxxViewController.m

UIImageView *logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(90, 12, 133, 62)];
logoImageView.image = [UIImage imageNamed:@"logo"];
[self.view addSubview:logoImageView];

yyyyViewController.m

UIImageView *logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(90, 12, 133, 62)];
logoImageView.image = [UIImage imageNamed:@"logo"];
[self.view addSubview:logoImageView];

同样适用于3+ viewController.m个文件。这是我的问题,我们怎样才能在IOS新手的整个项目中重复使用这段代码我无法弄清楚。

4 个答案:

答案 0 :(得分:1)

您可以创建一个LogoImageView类(UIImageView的子类),该类已配置为您希望如何使用它。然后,您可以在任何需要的地方添加它。


LogoImageView.h:

#import <UIKit/UIKit.h>

@interface LogoImageView : UIImageView

+ (UIImageView *)defaultView;

@end

LogoImageView.m:

@implementation LogoImageView

- (id)init {
    self = [super initWithFrame:CGRectMake(90, 12, 133, 62)];
    if (self){
        self.image = [UIImage imageNamed:@"logo"];
    }
    return self;
}

+ (UIImageView *)defaultView {
    return [[self alloc] init];
}

@end

你可以像这样在任何地方使用它(确保导入LogoImageView.h):

[view addSubview:[LogoImageView defaultView]];

答案 1 :(得分:1)

您可以创建可以添加子视图的UIView类别:

-(void)addLogo {
    UIImageView *logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(90, 12, 133, 62)];
    logoImageView.image = [UIImage imageNamed:@"logo"];
    [self addSubview:logoImageView];
} 

每当您想添加徽标时,您都可以致电:

[self.view addLogo];

答案 2 :(得分:1)

如何将代码提取为更可重用的形式,完全取决于视图控制器的性质。

如果所有视图控制器都显示类似的概念,所有视图控制器都包含徽标,我建议创建一个视图控制器超类:

@interface MyViewControllerWithLogo : UIViewController
@end

@implementation
- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView *logoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(90, 12, 133, 62)];
    logoImageView.image = [UIImage imageNamed:@"logo"];
    [self.view addSubview:logoImageView];
}
@end

如果视图控制器显示不同的概念,但它们包含相同类型对象的视图,则可以创建UIView子类并在需要带徽标的视图的任何地方使用它:

@interface MyViewWithLogo : UIView
@end

@implementation
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews
{
    UIImageView *logoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(90, 12, 133, 62)];
    logoImageView.image = [UIImage imageNamed:@"logo"];
    [self.view addSubview:logoImageView];
}
@end

其他常用方法不会真正为您节省任何代码,只会使其更复杂。另一种方法包括使用一个presenter对象,该对象将您想要添加徽标的视图添加到您将在每个视图控制器中实例化的内容。

@interface LogoPresenter : NSObject
    - (void)addLogoToView:(UIView *)view;
@end

@implementation
- (void)addLogoToView:(UIView *)view
{
     UIImageView *logoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(90, 12, 133, 62)];
    logoImageView.image = [UIImage imageNamed:@"logo"];
    [view addSubview:logoImageView];
}
@end

@implementation AViewController
- (void)viewDidLoad
{
    [super viewDidLoad];

    LogoPresenter *presenter = [LogoPresenter new];
    [presenter addLogoToView:self.view];
}
@end

你需要考虑的重要事情是在任何特定的地方做子类是否合乎逻辑。它们是coupling的最高形式,因此请谨慎使用子类。如果子类没有意义,请使用presenter方法。

答案 3 :(得分:0)

您可以创建一个名为LogoViewController的超类,并使徽标imageView成为它的属性。然后将所有设置代码移动到viewDidLoad,或者适当的地方。然后所有其他视图控制器都可以对它进行子类化,当它们调用[super viewDidLoad]时,它们将获得徽标设置代码。

相关问题