如何以面向对象的方式重用此ViewController代码?

时间:2015-06-13 17:22:23

标签: ios objective-c oop

我有一个带有四个viewControllers的应用程序,并且有很多代码在四个视图控制器中以完全相同的方式重复,我想知道什么是最好的OOP方式来编写这段代码只有一次并在其他视图控制器中重用它。

这是我第一个视图控制器的viewDidLoad中的代码:

- (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];
}

这是我viewDidLoad的{​​{1}}方法的代码,与第一个方法非常相似。我所有视图控制器的差异都与那些差异相同。

secondViewController

1 个答案:

答案 0 :(得分:2)

重构此代码是一个非常好的主意;这是DRY的一个例子。在这种情况下,最好的方法是使用继承:在ViewController类中编写所有公共代码,例如

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
    ...
}

并让您的其他视图控制器继承自ViewController(而不是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"]];
    ...
}