简体   繁体   English

从Cocoa中的视图访问子视图的drawRect方法

[英]Accessing drawRect method of a subview from a view in Cocoa


I have my main iOS view, with an UIVIew as subview linked to a class (called graphView). 我有我的主要iOS视图,有一个UIVIew作为子视图链接到一个类(称为graphView)。 My graphView has a drawRect method that is initiated upon application start. 我的graphView有一个drawRect方法,该方法在应用程序启动时启动。

Now, clicking on a button in the main view, I would like the sub UIView to refresh (so drawRect restarted) with specific parameters. 现在,单击主视图中的按钮,我希望使用特定参数刷新子UIView(以重新启动drawRect)。

How can I do that, ie how can I access the graphView class that was initially instanced ? 我该怎么做,即如何访问最初实例化的graphView类?

Thanks 谢谢

You can request to update the view by calling setNeedsDisplay on the UIView. 您可以通过在UIView上调用setNeedsDisplay来请求更新视图。 Then the drawRect method should be called by the OS. 然后,操作系统应调用drawRect方法。

If you have created your subview in XCode designer, create an outlet of the UIView in the UIViewDelegate to get access to the subview instance. 如果您已在XCode设计器中创建了子视图,请在UIViewDelegate中创建UIView的出口以访问子视图实例。

You should have a property for your GraphView. 您应该为GraphView具有一个属性。 Through this you can always access your Graphview inside your main viewcontroller, ie: 通过此操作,您始终可以在主视图控制器中访问Graphview,即:

@property (strong, nonatomic) GraphView *graphView

which you would synthesize like this in your implementation: 您可以在实现中像这样合成:

@synthesize graphView = _graphView;

When a user now presses a button you could execute the following function: 用户现在按下按钮时,可以执行以下功能:

- (void) buttonTouched:(id)sender
{
    [_graphView setNeedsDisplay];
}

This will mark _graphView as a view that needs to be redrawn, thus executing its drawRect: method in the next draw-cycle. 这会将_graphView标记为需要重绘的视图,从而在下一个绘制循环中执行其drawRect:方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM