Reputation: 56509
I've been going through an IOS project, in which I found a same set of code used in multiple viewController
.
Example:
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];
Similarly for 3+ viewController.m
files. Here is my question, how can we reuse this piece of code throughout the entire project being new to IOS I can't figure it out.
Upvotes: 1
Views: 160
Reputation: 94723
How you want to extract the code into a more reusable form, depends completely on the nature of the view controllers.
If all the view controllers are displaying a similar concept that all include a logo I would recommend creating a view controller superclass:
@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
If the view controllers are displaying different concepts, but they contain views for the same sort of object, you can create a UIView subclass and use that wherever you need the view with the logo:
@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
Other common methods are not going to really save you any code and only make it more complicated. Another method would include using a presenter object that takes the view that you want to add the logo to that you would instantiate in each view controller.
@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
The big thing you need to think about is do subclasses in any particular place make logical sense. They are the highest form of coupling so use subclassing with caution. If subclasses don't make sense, go with the presenter method.
Upvotes: 1
Reputation: 11834
You can just create a LogoImageView class (subclass of UIImageView) that is configured how you'd like to use it. Then you can add it everywhere you need.
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
And you can use it everywhere like this (make sure you import LogoImageView.h
):
[view addSubview:[LogoImageView defaultView]];
Upvotes: 1
Reputation: 25459
You can create UIView category which can add subview:
-(void)addLogo {
UIImageView *logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(90, 12, 133, 62)];
logoImageView.image = [UIImage imageNamed:@"logo"];
[self addSubview:logoImageView];
}
and whenever you want to add logo you can call:
[self.view addLogo];
Upvotes: 1
Reputation: 466
You could create a superclass called LogoViewController and make the logo imageView a property of it. Then move all the setup code to viewDidLoad, or wherever is appropriate. Then all the other view controllers can subclass it and when they call [super viewDidLoad] they will get the logo setup code.
Upvotes: 0