gpt4 book ai didi

ios - 如何以面向对象的方式重用这个 ViewController 代码?

转载 作者:行者123 更新时间:2023-11-28 21:44:25 26 4
gpt4 key购买 nike

我有一个带有四个 View Controller 的应用程序,并且有很多代码在四个 View Controller 中以完全相同的方式重复,我想知道只编写一次此代码的最佳 OOP 方法是什么并在其他 View Controller 中重用它。

这是我的第一个 View Controller 的 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];
}

这是我的 secondViewControllerviewDidLoad 方法的代码,与第一个非常相似。我所有 View Controller 的区别都与那些相同。

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

}

最佳答案

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

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

并让您的其他 View Controller 继承自 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"]];
...
}

关于ios - 如何以面向对象的方式重用这个 ViewController 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30821472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com