开发者

Custom control doesn't display in window

I'v created view based application. And Added new class which extends from UIView. This class contains some controls in it (simple controls). Now I want to display/hide this control when user click on something. Control should be displayed as a overlay window. Over all other controls. I try to do this like:

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

Everything goes fine, debug get in custom control code but nothing display on screen. I tried to add it manually and it works, but I need to add it in runtime. What could be the reason for 开发者_Go百科not displaying control?

Complete code for 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];  
}

And it's header:

@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


You probably want to make the call like this:

[self.view addSubview:myOverlayChannelPicker];

By the look of your posted code sample I have to assume that you have not been doing any objective-c development before. If that is the case you should probably start here Introduction to The Objective-C Programming Language to get the basics covered.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜