0

我有一个 UIViewController 并且我希望它调用 drawRect 以便我可以在视图上绘制但没有任何反应。

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
...code...
}

@end

和实施

#import "ViewController.h"


@implementation ViewController

-(void) drawRect:(CGRect) rect {
draw a pony
}

- (void)viewDidLoad {
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

但是当应用程序运行时,视图上没有绘制任何小马,我做错了什么?

4

2 回答 2

6

UIViewController没有被覆盖的消息drawRect。您应该创建从那里派生的自定义类UIView并覆盖消息drawRect

您可以覆盖消息(UIView *)viewUIViewController返回自己的自定义UIView或在 Interface Builder 中将类从自己的类更改UIView为自己的类。

于 2010-06-24T11:50:47.830 回答
0

调用setNeedsDisplay您的UIView,而不是ViewController,是触发和重新触发drawRect代码的一种方式,您可以在需要更新整个视图时有条件地发送此消息。

UIView您可以使用自定义在自定义中调用它

[self.setNeedsDisplay]

或从 View Controller 通过创建控制器UIView的属性并调用

[self.myView setNeedsDisplay]
于 2012-08-27T18:51:32.467 回答