我是一名新手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/