- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试通过分离属于 UIView
的代码来重组早期项目来自属于 UIViewController
的代码.一个热门问题 (found here) 的答案似乎没有解决我需要做的事情,所以让我用两个例子来说明我的问题。
这里是方法 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;
}
}
在下面的代码中,我尝试在 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:zone
在 CustomView
无法引用 zone
由 setSelected: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/
我希望能够在代码中布局我的 View Controller ,但可以看到界面构建器中显示的布局。 我知道我可以创建一个 UIView 子类,使其 IBDesignable,并将其分配给 View Co
我有一个父 UIViewController(MainVC)。从那里我有 2 个 segue 到 2 个 UIViewControllers:FirstVC(标识符:goFirstVC)和 Secon
我有一个导航 Controller 。第一个viewcontroller 是FirstViewController 的一种。当我在 FirstViewController 中点击一个按钮时,它会将第二
我有一个自定义的动画 UIViewController 过渡,似乎 iOS 中有一个错误会破坏横向布局。在主要动画方法中,我得到了横向和纵向 View 的混合。 (纵向 View 都是纵向的,所以没问
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我有一个 UIViewController,里面有 UITabBar。我正在尝试模仿 UITabBarController。 My question is how do I set or a UIVi
是否有任何方便的方法来确定是否正在从处于后台模式的应用加载 View ? 在 3.X 中,我将依赖 viewDidLoad 进行一些初始化等操作,但是对于 4.X 而言情况并非如此,因为您不能依赖调用
我将主视图 Controller 作为带有 UICollectionView 的 View Controller ,并且填充了照片。 一旦一个单元格被点击,一个新的 View Controller 被
我有一个简单的 UIViewController(单点触控),在 UI 上有一个按钮。现在,我的用例很简单 - 我想在单击按钮时重新加载 UIViewController。在某些线程上,我读到我只需要
我在它上面有一个 UIViewController 1 UIButton(添加为 subview ),在我按下 Button(见下面的图 1)之后,在它上面添加了另一个 UIViewControlle
尝试为 iPad 制作基于 Storyboard的应用程序。在其中,我需要从开始屏幕 (UIViewController) 过渡到主屏幕 (UISplitViewController),然后再到全屏
我想调整 UIViewController 的大小在 Storyboard中,所以我可以使用它的 UIViewController作为弹出 View 。 在大多数网站上,我可以读到这些操作是: 拖一个
我正在尝试从 XIB 或 Storyboard加载 ViewModel 绑定(bind)的 ViewController(使用 MvvmCross 5.0.6)。我的应用程序的逻辑是,该 View C
我正在构建一个锻炼应用程序。我的 TableViewController 中有一系列练习,每个单元格显示不同的练习。单元格转到 UIViewController。在 UIViewController
我正在尝试从 XIB 或 Storyboard加载 ViewModel 绑定(bind)的 ViewController(使用 MvvmCross 5.0.6)。我的应用程序的逻辑是,该 View C
我有一个 UIViewController,它有一个模式窗口,我想在整个界面上展示它,包括 UITabBar。 我的应用程序层次结构是这样的: UITabBarController (A) ->
我想显示另一个 ViewController 的缩小“预览”在另一个 ViewController .这可能吗?不需要交互,因此它基本上可以是屏幕截图,但按比例缩小。我可以通过手动截屏并将其保存为图像
我正在开发一个应用程序,其中 UIViewController ( firstViewController ) 包含一些 UILabels , 一个 UIButton , 和 UIView ( sub
我的标签栏中有五个标签栏项目。第五项是弹出ViewController。因此,当我单击该按钮时,当前 Controller 中将显示一个弹出窗口。我使用 UIViewController 作为子类来实
我正在开发 iOS 应用程序。 我的应用程序有 6 个按钮,每个按钮都有每个标签栏控件。 so Main screen is [btn1] [btn2] [btn3] [btn4] [btn5] [b
我是一名优秀的程序员,十分优秀!