自定义控件不会显示在窗口中

时间:2010-12-01 14:15:10

标签: iphone objective-c uiview uiwindow

我创建了基于视图的应用程序。并添加了从UIView扩展的新类。该类包含一些控件(简单控件)。现在我想在用户点击某些内容时显示/隐藏此控件。控件应显示为叠加窗口。超过所有其他控件。我尝试这样做:

- (void)viewDidLoad {
    [super viewDidLoad];        
    addSubview:myOverlayChannelPicker;
}

一切顺利,调试获取自定义控制代码,但屏幕上没有显示。我尝试手动添加它,它工作,但我需要在运行时添加它。可能是不显示控件的原因是什么?

OverlayChannelPicker的完整代码

@implementation OverlayChannelPicker
@synthesize myImage;
@synthesize img1;
@synthesize img2;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib{
    img1 = [UIImage imageNamed:@"imgTest.jpg"];
    img2 = [UIImage imageNamed:@"imgTest2.jpg"];

    arrayOfImages = [[NSMutableArray alloc] init];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];

    scrollView = [[UIScrollView alloc] init];
    scrollView.scrollEnabled = YES;
    scrollView.pagingEnabled = NO; // for smooth scroll
    scrollView.directionalLockEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    //scrollView.backgroundColor = [UIColor blueColor];
    scrollView.autoresizesSubviews = YES;
    scrollView.frame = CGRectMake(0, 150, 320, 128);
    [scrollView setContentSize:CGSizeMake(1620, 128)];
    [self addSubview:scrollView];

    UIImage *imageToAdd;
    int x = 0;
    int y = 0;
    for(imageToAdd in arrayOfImages)
    {       
        UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];     

        temp.frame = CGRectMake(x, y, 128, 128);
        temp.userInteractionEnabled = YES;      
        x += 135;


        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
        [temp addGestureRecognizer:tap];    

        [scrollView addSubview:temp];
    }

}

- (void)imageTapped:(UIGestureRecognizer *)sender
{       
    UIImageView *iv = (UIImageView *)[sender view];
    UIImage *image = [iv image];
    UIImage *it = [arrayOfImages indexOfObject:image];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:iv cache:YES];

    if(it == img1)
        it = img2;
    else
        it = img1;

    [UIView commitAnimations];  
}

它是标题:

@interface OverlayChannelPicker : UIView <UIScrollViewDelegate>{
    IBOutlet UIImageView *myImage;
    UIImage *img1;
    UIImage *img2;  

    IBOutlet UIScrollView *scrollView;
    int view1Index;
    int view2Index; 
    NSMutableArray *arrayOfImages;

}
@property (nonatomic, retain) UIImageView *myImage;
@property (nonatomic, retain) UIImage *img1;
@property (nonatomic, retain) UIImage *img2;
@property (nonatomic, retain) UIScrollView *scrollView;

- (void)awakeFromNib;
@end

1 个答案:

答案 0 :(得分:0)

你可能想这样打电话:

[self.view addSubview:myOverlayChannelPicker];

通过您发布的代码示例,我必须假设您之前没有进行任何Objective-c开发。如果是这种情况,您应该从Introduction to The Objective-C Programming Language开始,以了解基础知识。