How to reuse this ViewController code in an object oriented way?

Alfro

I have an app with four viewControllers, and there is a lot of code that is repeating exactly the same way in the four view controllers, and I would like to know what is the best OOP way to write this code only one time and reuse it in the others view controllers.

This is the code from viewDidLoad of my first view controller:

- (void)viewDidLoad {

[super viewDidLoad];
[self setCanDisplayBannerAds:YES];

[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setImage:[UIImage imageNamed:@"iconoBota30x30.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setImage:[UIImage imageNamed:@"actividades.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:3] tabBarItem]setImage:[UIImage imageNamed:@"nosotros.png"]];


float width = [UIScreen mainScreen].bounds.size.width;

float height = [UIScreen mainScreen].bounds.size.height;

static_iVar = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, width,height)];
static_iVar.delegate = self;
NSURL *url = [NSURL URLWithString:@"http://guiasdelsur.es"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[static_iVar loadRequest:requestObj];
[self.view addSubview:static_iVar];


//GESTURES

//DOWN
UISwipeGestureRecognizer *gest1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hideTabBar:)];
gest1.delegate = self;
[gest1 setDirection:UISwipeGestureRecognizerDirectionDown];
[self.view addGestureRecognizer:gest1];

//UP
UISwipeGestureRecognizer *gest2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(showTabBar:)];
gest2.delegate = self;
[gest2 setDirection:UISwipeGestureRecognizerDirectionUp];
[self.view addGestureRecognizer:gest2];


//first tab

_tabInicio = [[UITabBarItem alloc] initWithTitle:@"Inicio" image:[UIImage imageNamed:@"d.png"] tag:0];

[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];
[self.tabBarController setSelectedIndex:0];

self.tabBarItem = _tabInicio;




_cToolBar = [[UIToolbar alloc] init];
_cToolBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];


[_cToolBar setBarStyle:UIBarStyleBlack];
[_cToolBar setTranslucent:YES ];
[_cToolBar setTintColor:[UIColor greenColor]];


// Do any additional setup after loading the view, typically from a nib.


NSString *backArrowString = @"\U000025C0\U0000FE0E Atrás"; //BLACK LEFT-POINTING TRIANGLE PLUS VARIATION SELECTOR
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:backArrowString style:UIBarButtonItemStyleDone target:nil action:@selector(goBack)];


UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:nil action:@selector(refreshControl)];
[right setTintColor:[UIColor greenColor]];

UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];


[items addObject:back];
[items addObject:flexibleSpace];
[items addObject:right];



[_cToolBar setItems:items animated:NO];

[self.view addSubview:_cToolBar];


//iAD

ADBannerView * adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, 44);
adView.delegate = self;
[self.view addSubview:adView];



//loading indicator
_indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_indicator setCenter:self.view.center];
[_indicator setHidesWhenStopped:YES];
[self.view addSubview:_indicator];
[_indicator startAnimating];
}

And this is the code from viewDidLoad method of my secondViewController, very similar to the first one. The differences in all my view controllers are the same than those ones.

- (void)viewDidLoad     {
    [super viewDidLoad];
    [self setCanDisplayBannerAds:YES];
    float width = [UIScreen mainScreen].bounds.size.width;

    float height = [UIScreen mainScreen].bounds.size.height;
    static_iVar2 = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, width,height)];

    static_iVar2.delegate = self;
    NSURL *url = [NSURL URLWithString:@"http://guiasdelsur.es/category/programas/"];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [static_iVar2 loadRequest:requestObj];
    [self.view addSubview:static_iVar2];



    //second tab

    _secondTab = [[UITabBarItem alloc] initWithTitle:@"Programas" image:[UIImage imageNamed:@"iconoBota30x30.png"] tag:1];

    [[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];
    [self.tabBarController setSelectedIndex:1];
    self.tabBarItem = _secondTab;




    //GESTURES

    //DOWN
    UISwipeGestureRecognizer *gest1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hideTabBar:)];
    gest1.delegate = self;
    [gest1 setDirection:UISwipeGestureRecognizerDirectionDown];
    [self.view addGestureRecognizer:gest1];

    //UP
    UISwipeGestureRecognizer *gest2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(showTabBar:)];
    gest2.delegate = self;
    [gest2 setDirection:UISwipeGestureRecognizerDirectionUp];
    [self.view addGestureRecognizer:gest2];



    UIToolbar *cToolBar = [[UIToolbar alloc] init];
    cToolBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
    NSMutableArray *items = [[NSMutableArray alloc] init];


    [cToolBar setBarStyle:UIBarStyleBlack];
    [cToolBar setTranslucent:YES ];
    [cToolBar setTintColor:[UIColor greenColor]];

    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil ];

    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:nil action:@selector(refreshControl)];
    [right setTintColor:[UIColor greenColor]];


    [items addObject:flexibleSpace];
    [items addObject:right];




    [cToolBar setItems:items animated:NO];
    [self.view addSubview:cToolBar];

    //iAD

    ADBannerView * adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    adView.frame = CGRectOffset(adView.frame, 0, 44);
    adView.delegate = self;
    [self.view addSubview:adView];


    //Indicator


    _indicador = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [_indicador setCenter:self.view.center];
    [_indicador setHidesWhenStopped:YES];

    [self.view addSubview:_indicador];
    [_indicador startAnimating];

}
Glorfindel

It is a very good idea to refactor this code; it is an example of DRY. The best way in this case would be to use inheritance: write all common code in the ViewController class, e.g.

ViewController.h

@interface ViewController : UIViewController {
    // Variables which are declared here can also be used in the subclasses
}

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setCanDisplayBannerAds:YES];
    // more common code
    ...
}

and have your other view controllers inherit from ViewController (rather than UIViewController):

FirstViewController.h

@interface FirstViewController : ViewController
...

FirstViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    // more specific code
    [[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setImage:[UIImage imageNamed:@"iconoBota30x30.png"]];
    [[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setImage:[UIImage imageNamed:@"actividades.png"]];
    [[[self.tabBarController.viewControllers objectAtIndex:3] tabBarItem]setImage:[UIImage imageNamed:@"nosotros.png"]];
    ...
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I design this procedural code as class based (object oriented)?

分類Dev

How to reuse code in an R function?

分類Dev

Code explanation - object-oriented programming

分類Dev

How to model an object oriented design in a database?

分類Dev

Object-oriented way of doing nil checks for associations in rails

分類Dev

ios:How to persist object value in viewcontroller

分類Dev

How to reuse Swift typealias in Objective-C code

分類Dev

How can I make a switch statement more object oriented?

分類Dev

How to structure object-oriented Python 3 project and its imports?

分類Dev

Selenium Page Object Reuse

分類Dev

Python Object Oriented

分類Dev

Vue - best way to reuse methods

分類Dev

Can we reuse a Gson object?

分類Dev

Object Oriented adding items to an a arrayList

分類Dev

Python object oriented design concepts

分類Dev

Private Property - Object Oriented PHP

分類Dev

How to reuse Jasmine tests

分類Dev

How to reuse yield on spider

分類Dev

How to reuse Custom UICollectionView?

分類Dev

How to reuse QFile?

分類Dev

How to reuse code block which describe similiar ant build logic in groovy?

分類Dev

Object oriented programming in c# object definition

分類Dev

Is there a way to reuse an ImageData to reduce GC activity?

分類Dev

What is the correct way to store, increment and reuse an index

分類Dev

how to change for loop code to pythonic way

分類Dev

Reuse a property value as a variable within the same object

分類Dev

How to reuse autocalculated property in Pyventory?

分類Dev

How to reuse the same Watermark TextBox

分類Dev

How to reuse Angular resource functions?

Related 関連記事

ホットタグ

アーカイブ