开发者

Display UIView subclass which contains other UIViews

I want to package some UIViews suchs as UIButton, UIImageView in a single UIView. When I try to display that view, it is not showing up on my RootViewController:

Here is the code of my UIView subclass :

#import "Hover.h"

@implementation Hover


- (id)init{
    self = [super init];
    if (self)开发者_如何学Go {
        // 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;
}

And here is the RootViewController class:

- (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.
}

The "hover view" is not displayed! However, when I add a single UIImageView to my RootViewController, it works!


In order for your view to display, you should override the -(id)initWithFrame:(CGRect)frame;, instead of writing a custom initializer.

Try this way :

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

Also, in the -(void)viewDidLoad; method, first send [super viewDidLoad];, and then alloc/init the view.


        - (void)viewDidLoad
        {
            [super viewDidLoad];

            Hover *hover = [[Hover alloc] init];

            [self.navigationController.visibleViewController.view addSubview:hover];

            [hover release];



                // Do any additional setup after loading the view, typically from a nib.
        }


Just a quick guess at a glance: If you're using a navigationcontroller, you should initWithRootViewController. You can't add a view to a navigationcontroller directly, Use push instead.

HTH Marcus

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜