ios - 重写 UIView 子类中的 drawRect 方法并将其调用到另一个 UIViewcontroller 中

标签 ios ipad core-graphics quartz-2d

我是一名新手ios程序员,这是我的第一个项目。我将专门为ipad开发这个项目。在这个项目中,我需要通过解析 xml 元素来绘制几个圆圈并在其上显示数据。我已经通过子类化 UIView 类并覆盖 drawRect 方法完成了圆圈绘制部分。我通过 loadview 方法在 UIViewcontroller 中加载 UIView 子类。现在我需要做的是

  • 触摸圆圈并切换到另一个 UIViewcontroller。

我不确定如何切换到另一个 UIViewcontroller,因为所有绘图和触摸检测代码都在我的 UIView 子类中。

如果有帮助,我们将不胜感激

最佳答案

您需要使用委托(delegate)方法告诉父 View Controller 有触摸,以便它可以呈现另一个 View Controller 。

UIView 子类 header 的顶部添加以下内容:

@protocol MyCustomViewDelegate <NSObject>

- (void)customViewCircleTapped;

@end

然后,在 View 声明中(自定义 View 子类的现有声明):

@interface MyCustomView : UIView

...

@property (weak) id<MyCustomViewDelegate> delegate;

之后,在 View Controller 中,您需要设置view.delegate = self,以便 View 可以引用 View Controller 。

然后,在 View Controller header 中,将声明更改为如下所示:

@interface MyViewController : UIViewController <MyCustomViewDelegate>

然后在 View Controller 实现中实现customViewCircleTapped:

- (void)customViewCircleTapped {
    ... // Open a view controller or something
}

完成此操作后,您可以在 View 中的触摸检测代码中添加:

[self.delegate customViewCircleTapped];

它的作用是让您的自定义 View 能够通过调用此方法告诉其父 View Controller 发生了某些事情(如果需要传递数据,您可以更改它并添加参数),然后 View Controller 可以打开另一个 View Controller 或基于此执行某些操作。

View detects touches → Process touches → call customViewCircleTapped delegate method on view controller → view controller opens another view controller

关于ios - 重写 UIView 子类中的 drawRect 方法并将其调用到另一个 UIViewcontroller 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16663021/

相关文章:

ios - 如何将html字符串保存到数据库中

iOS 8 键盘扩展 - UIInputViewController 无法添加多个 View

javascript - 在 iPad 上,如何在不显示选项的情况下通过 Javascript 事件在选择元素上设置 focus()?

iphone - CG上下文旋转

iphone - 以编程方式编写 UIButton 操作代码

ios - 打开和关闭特定 viewController 的声音

ios - 在 iPad 上,当浏览器为 'closed' 时,如何使网页上的嵌入声音停止播放?

iphone - 如何检测包含目标操作(对应于按钮单击)的 "viewcontroller"文件?

ios - 在 Swift 2 的绘图应用程序中创建一个 'undo' 函数

ios - 使用核心图形和核心动画创建包含圆圈的动画