gpt4 book ai didi

ios - 是否使用 getter 和 setter 将消息从父 UIViewController 传递到 UIView 类中的方法?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:54:51 25 4
gpt4 key购买 nike

我正在尝试通过分离属于 UIView 的代码来重组早期项目来自属于 UIViewController 的代码.一个热门问题 (found here) 的答案似乎没有解决我需要做的事情,所以让我用两个例子来说明我的问题。

  • 示例 1

这里是方法 setBackground:zone更改 View 的背景颜色以指示应用程序中的各种状态。 如下所示方法目前有效,我想将代码重新定位到它所属的 View 。

ViewController.h

#import <UIKit/UIKit.h>
#import "CustomView.h"

@interface ViewController : UIViewController {
}
@end

ViewController.m

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

int zone = 1; // or 2 or 3;

self.view = [[UIView alloc] initWithFrame: [UIScreen mainScreen].bounds];
[self setBackground:zone];
}

- (void)setBackground:(int)zone {
switch (zone) {
case 1:
self.view.backgroundColor = [UIColor orangeColor];
break;
case 2:
self.view.backgroundColor = [UIColor cyanColor];
break;
case 3:
self.view.backgroundColor = [UIColor greenColor];
break;
default:
break;
}
}
  • 示例 2

下面的代码中,我尝试在 CustomView 中初始化背景颜色通过使用 getter 和 setter 来引用 zone 的值在 ViewController (适用于原始项目中的 ViewControllers 已经获取并设置 zone 以更改背景颜色)。

自定义 View .h

#import <UIKit/UIKit.h>

@interface CustomView : UIView {
UIViewController *parent;
int selectedZone;
}
- (void)setParent:(UIViewController *)parent;
- (int)getSelectedZone;
@end

自定义 View .m

#import "CustomView.h"

@implementation CustomView
- (void)setParent:(UIViewController *)theParent {
parent = theParent;
}

- (int)getSelectedZone {
return selectedZone;
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:[UIScreen mainScreen].bounds];
if (self) {

NSLog(@"selectedZone in CustomView is seen as %i", [self getSelectedZone]);

int zone = [self getSelectedZone];
[self setBackground:(int) zone];
}
return self;
}

- (void)setBackground:(int)zone {
switch (zone) {
case 1:
self.view.backgroundColor = [UIColor orangeColor];
break;
case 2:
self.view.backgroundColor = [UIColor cyanColor];
break;
case 3:
self.view.backgroundColor = [UIColor greenColor];
break;
default:
break;
}
}

ViewController.h

#import <UIKit/UIKit.h>
#import "CustomView.h"

@interface ViewController : UIViewController {
int selectedZone;
}

- (void)setSelectedZone:(int)zone;
- (int)getSelectedZone;

ViewController.m

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

int zone = 1; // or 2 or 3;
[self setSelectedZone:(int)zone];
NSLog(@"selectedZone in ViewController is now set to %i", [self getSelectedZone]);

self.view = [[UIView alloc] initWithFrame: [UIScreen mainScreen].bounds];

CustomView *cv = [[CustomView alloc]init];
[self.view addSubview:cv];
}

- (void)setSelectedZone:(int)zone {
selectedZone = zone;
}

- (int)getSelectedZone {
return selectedZone;
}

我可以告诉我上面的代码不起作用,因为getSelected:zoneCustomView无法引用 zonesetSelected:zone 设置在ViewController.但我不明白为什么。

2017-04-05 07:04:26.126 ZoneIndicator[1865:1270743] selectedZone in ViewController is now set to 1 
2017-04-05 07:04:26.126 ZoneIndicator[1865:1270743] selectedZone in CustomView is seen as 0

但是an article found here甚至让我怀疑使用 getter 和 setter 是否是最好的方法 - 特别是这个:

The biggest danger here is that by asking for data from an object, you are only getting data. You’re not getting an object—not in the large sense. Even if the thing you received from a query is an object structurally (e.g., a String) it is no longer an object semantically. It no longer has any association with its owner object. Just because you got a string whose contents was “RED”, you can’t ask the string what that means. Is it the owners last name? The color of the car? The current condition of the tachometer? An object knows these things, data does not.

那么如何将消息从父 UIViewController 传递到 UIView 类中的方法?

最佳答案

vc 设置它的 View 属性是完全可以的。这是您的第一个示例的注释代码...

- (void)viewDidLoad {
[super viewDidLoad];

// no need to do this, the UIViewController this inherits from creates the view
// int zone = 1; // or 2 or 3;
//self.view = [[UIView alloc] initWithFrame: [UIScreen mainScreen].bounds];
[self setBackgroundColorForZone:zone];
}

// improved naming for clarity
- (void) setBackgroundColorForZone:(NSInteger)zone {
// fine as you have it
switch //...
// ...
}

如果您有更好的理由开发自定义 UIView 子类作为该 View Controller 的 View ,那也没关系。您可以用自定义实例替换您的 View ,或者用具有相同框架的 subview 覆盖默认 View 。但是给 View 一个指向它的 View Controller 的指针是不明智的(更不明智的是称这个指针为“父”)

所以对于你的第二个例子,自定义 View 类应该大大简化......

@interface CustomView : UIView {
// commented out bad stuff
// UIViewController *parent;
// this isn't needed either
//int selectedZone;
}
// these aren't needed either
//- (void)setParent:(UIViewController *)parent;
//- (int)getSelectedZone;

// just this
- (void)setBackgroundColorForZone:(NSInteger)zone;

@end

并且实现可以使用与第一个示例中相同的方法。它采用区域整数和设置的 self.backgroundColor(而不是 self.view.backgroundColor)。

管理这个 View 的 View Controller 现在可以简化为:

- (void)viewDidLoad {
[super viewDidLoad];

int zone = 1; // or 2 or 3;
// don't need this
//[self setSelectedZone:(int)zone];
//NSLog(@"selectedZone in ViewController is now set to %i", [self getSelectedZone]);

// never need this
//self.view = [[UIView alloc] initWithFrame: [UIScreen mainScreen].bounds];

// notice the change to init with frame
CustomView *cv = [[CustomView alloc]initWithFrame:self.view.bounds];
[cv setBackgroundColorForZone:zone];
[self.view addSubview:cv];
}

// don't need any of this
//- (void)setSelectedZone:(int)zone {
// selectedZone = zone;
//}

//- (int)getSelectedZone {
// return selectedZone;
//}

关于ios - 是否使用 getter 和 setter 将消息从父 UIViewController 传递到 UIView 类中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43218839/

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