• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - 如果 View 是属性,则不执行 InitWithFrame

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:58:41 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有继承自 UIView 的 GraphicView 类。它的initWithFrame方法是:

@implementation GraphicsView

- (id)initWithFrameCGRect)frameRect
{
    self = [super initWithFrame:frameRect];

    // Create a ball 2D object in the upper left corner of the screen
    // heading down and right
    ball = [[Object2D alloc] init];
    ball.position = [[Point2D alloc] initWithX:0.0 Y:0.0];
    ball.vector = [[Vector2D alloc] initWithX:5.0 Y:4.0];

    // Start a timer that will call the tick method of this class
    // 30 times per second
    timer = [NSTimer scheduledTimerWithTimeInterval1.0/30.0)
                                             target:self
                                           selectorselector(tick)
                                           userInfo:nil
                                            repeats:YES];

    return self;
}

使用 Interface Builder 我已将 UIView (class = GraphicView) 添加到 ViewController.xib。我添加了 GraphicView 作为属性:

@interface VoiceTest01ViewController : UIViewController {

    IBOutlet GraphicsView *graphView;
}

@property (nonatomic, retain) IBOutlet GraphicsView *graphView;

- (IBAction)btnStartClickedid)sender;
- (IBAction)btnDrawTriangleClickedid)sender;

@end

但是这段代码不起作用,我需要调用 [graphView initWithFrame:graphView.frame] 使其起作用。

- (void)viewDidLoad {
    [super viewDidLoad];
    isListening = NO;
    aleatoryValue = 10.0f;

    // Esto es necesario para inicializar la vista
    [graphView initWithFrame:graphView.frame];

}

我过得好吗?有没有更好的方法来做到这一点?

我不知道为什么添加 GraphicView 作为属性时不会调用 initWitFrame。



Best Answer-推荐答案


initWithFrame 在从 NIB 加载时不会被调用,而是 initWithCoder

如果您可能同时使用从 NIB 加载和编程创建,您应该创建一个通用方法(initCommon 也许?)您将从 initWithFrameinitWithCoder.


哦,你的 init 方法没有使用推荐的做法:

- (id)initWithFrameCGRect)frameRect
{
    if (!(self = [super initWithFrame:frameRect]))
        return nil;

    // ...
}

你应该经常检查[super init...]的返回值。

关于iphone - 如果 View 是属性,则不执行 InitWithFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6356162/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap