gpt4 book ai didi

iphone - ViewController 和绘制矩形

转载 作者:行者123 更新时间:2023-12-03 17:29:47 25 4
gpt4 key购买 nike

我有一个 UIViewController,我希望它调用drawRect,这样我就可以在 View 上绘图,但什么也没有发生。

#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

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

最佳答案

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

您可以覆盖 UIViewController 的消息 (UIView *)view 并返回自己的自定义 UIView 或在 Interface Builder 中从 更改类UIView 拥有自己的类。

关于iphone - ViewController 和绘制矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3109529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com