iOS:如何让一个视图触发另一个视图的更新? [英] iOS: how can I make one view trigger an update in another?
问题描述
我的顶层 UIView
包含一些标签和绘图画布.绘图画布本身是一个 UIView
,它覆盖了它的 drawRect()
method,当然还有各种 touch 方法.当用户完成绘图后,我想更新画布外的一个或多个标签(即都是顶级 UIView
的所有组件).
My top level UIView
contains a some labels and a drawing canvas. The drawing canvas is itself a UIView
which overrides its drawRect()
method and, of course, the various touch methods. When the user has finished drawing, I want to update one or more labels that are outside the canvas (i.e. are all components of the top level UIView
).
最好的方法是什么?
我目前有顶级 UIViewController
将各种标签从顶级 传递到我的
method 并且我将它们存储在本地.每当我需要更新标签时,我都会从我拥有的这些兄弟视图的本地副本中进行更新 - 但我对此并不满意,因为它感觉很笨拙.CanvasView
sviewDidLoad()
What's the best way to do it?
I currently have the top level UIViewController
pass the various labels into my CanvasView
s from the top level viewDidLoad()
method and I store them locally. Whenever I need to update a label, I do so from the local copies of these sibling views I have - But I'm not happy with this because it feels kludgy.
为了进一步解释,这就是我在顶级 UIViewController
中所做的:
To explain further, this is what I do in my top level UIViewController
:
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
for v in self.view.subviews {
if v .isKindOfClass(CanvasView) {
let canvasView = v as CanvasView
canvasView.setMyLabel(myLabel)
}
}
}
然后在我的 CanvasView.swift
文件中,我有:
And then in my CanvasView.swift
file, I have:
private var myLabel: UILabel!
...
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
// do stuff ...
setNeedsDisplay()
myLabel.text = "etc.etc."
}
有没有更好的方法?
谢谢.
大道
推荐答案
如果您的画布是一个单独的 viewController
,您可以使用委托协议从画布视图控制器访问您的基本视图控制器.假设您有一个 BaseViewController
调用 canvasViewController
.
If your canvas is a separate viewController
, you can use delegate protocols to access your base view controller from the canvas view controller.
Suppose you have a BaseViewController
which calls the canvasViewController
.
在您的 canvasViewController.h
中添加以下内容:
In your canvasViewController.h
add the following :
@protocol CanvasDelegate <NSObject>
@required
//examples . add your methods to upgate labels here
- (void) updateLabels: (NSString*) labelText1 and:(NSString*)labelText2;
@end
@interface canvasViewController : UIViewController
{
id<CanvasDelegate> delegate;
}
@property (retain) id delegate;
@end
现在从你的 canvasViewController.m
文件中执行这段代码,只要你想更新你的标签(或要求 baseViewController 做一些事情):
Now from your canvasViewController.m
file execute this code whenever you want to update your labels (or ask the baseViewController to do something) :
[[self delegate] updateLabels:string1 and:string2];
现在为了让您的基本视图控制器接受来自画布的消息,BaseVC 必须采用"CanvasDelegate
协议并实现所需的方法.在 baseViewController.h
中,添加以下内容:
Now for your base view controller to accept messages from the canvas, the BaseVC has to "adopt" the CanvasDelegate
Protocol and also implement the required methods.
In baseViewController.h
, add the following :
@interface baseViewController : UIViewController <CanvasDelegate>
在baseViewController.m
中为pushing创建canvasViewController对象时,添加如下命令:
In baseViewController.m
when you create an object of canvasViewController for pushing, add the following command :
canvasVC.delegate = self;
并且不要忘记在 baseViewController.m
中实现所需的方法,如下所示:
And don't forget to implement the required methods in baseViewController.m
like so :
- (void) updateLabels: (NSString*) labelText1 and:(NSString*)labelText2
{
//update your labels here
}
这篇关于iOS:如何让一个视图触发另一个视图的更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!