objective-c - 显示包含其他 UIView 的 UIView 子类

标签 objective-c ios4

我想将一些UIView(例如UIButton、UIImageView)打包在单个UIView中。 当我尝试显示该 View 时,它没有显示在我的 RootViewController 上:

这是我的 UIView 子类的代码:

#import "Hover.h"

@implementation Hover


- (id)init{
    self = [super init];
    if (self) {
        // Initialization code
        UIImage *hover = [UIImage imageNamed:@"Hover.png"];
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = hover;
        imageView.frame = CGRectMake(0, 0, hover.size.width, hover.size.height);
        imageView.alpha = 0.75;
        [self addSubview:imageView];
        [imageView release];
    }
    return self;
}

这是 RootViewController 类:

- (void)viewDidLoad
{

    Hover *hover = [[Hover alloc] init];
    [self.navigationController.view addSubview:hover];
    [hover release];


    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

不显示“悬停 View ”!但是,当我将单个 UIImageView 添加到 RootViewController 时,它就可以工作了!

最佳答案

为了显示 View ,您应该重写 -(id)initWithFrame:(CGRect)frame;,而不是编写自定义初始值设定项。

试试这个方法:

-(id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        // do init here...
    }
    return self;
}

另外,在-(void)viewDidLoad;方法中,首先发送[super viewDidLoad];,然后分配/初始化 View 。

关于objective-c - 显示包含其他 UIView 的 UIView 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6746300/

相关文章:

debugging - 什么是 “Interface Builder Cocoa Touch Tool”

iphone - 在真实 iPhone 上测试应用程序

ios - 仅从 NSXMLElement 获取文本值,不带子节点的文本

ios - 从 NSString 获取常用字符作为子字符串

ios - 隐藏标签栏容器 View

iphone - 如何在 iphone 应用程序中打开 iTunes 链接?

xml - iPhone/iPad 的 SOAP 和 XML 响应解析示例?

ios - 来自字符串的 MD5 哈希值不匹配

ios - 当应用程序在 ios 9 中处于 InActive 状态时如何处理静默推送通知?

iphone - UIImagePNGRepresentation() 和比例(iPhone 4 屏幕)