subview not visible

I have created a UIViewController with a UIScrollView. On the scrollView I have a page control. When I was testing it I was able to scroll horizontally through four different color pages. Now I need to add the actual views I need. In a different part of the project I created a line graph using coreplot. Now I have added made a new UIview and added the code for the line graph. I am now trying to replace the first page of the scrollView with the linegraph UIView. I am getting no errors however nothing is appearing on the screen either. It is just the default background of the scrollView. The scrollView still scrolls through the other pages correctly. I added a button programatically to the UIView to see if it had something to do with coreplot but I cannot see that either. Here is some of my code. I would appreciate any advice. Thanks.

The viewDidLoad method for my main viewController: - (void)viewDidLoad { [super viewDidLoad];

    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor],     [UIColor blueColor], [UIColor purpleColor], nil];

    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    self.alertsSubView = [[AlertsView alloc] initWithFrame:frame];
    [self.scrollView addSubview:_alertsSubView];


    for (int i = 1; i < numberOfGraphs; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;
        UIView *subview = [[UIView alloc] initWithFrame:frame];
        subview.backgroundColor = [colors objectAtIndex:i];
       [self.scrollView addSubview:subview];

        self.alertsSubView.delegate = self;

    _scrollView.delegate = (id)self;

    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count,   self.scrollView.frame.size.height);
}

Here is the code for the initWithFrame method from my UIView. All of the NSLog statements get run so I know everything is being called:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        NSLog(@"AlertsView initWithFrame");

        ///button for testing
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect buttonFrame = CGRectMake(0,0, frame.size.width, frame.size.height);
        self.button.frame = buttonFrame;
        [self.button addTarget:self
                        action:@selector(buttonTouched)
              forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.button];


        [self initPlot];
    }
    return self;
}

I am also adding the methods initPlot and ConfigureHost in case they are relevant:

-(void)initPlot {
    NSLog(@"into init plot");
    [self configureHost];
    [self configureGraph];
    [self configurePlots];
    [self configureAxes];
    NSLog(@"endo of init plot");
}

-(void)configureHost {
    NSLog(@"into configure host");
    self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc]         initWithFrame:self.bounds];
    self.hostView.allowPinchScaling = YES;
    [self addSubview:self.hostView];
}

I finally figured this out. Thanks for your suggestion @Eric Skroch. It actually led me down the path to the right answer even though it was not the problem. Basically to fix this problem I had to move all of the logic into the view controller and change the order of alot of the code. I am posting the relevant parts of my revised view controller here in case anyone runs across anything similar.

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [_alertsSubView initPlot];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor],   [UIColor blueColor], [UIColor purpleColor], nil];
    alertFrame.origin.x = self.scrollView.frame.size.width;
    alertFrame.origin.y = 0;
    alertFrame.size = self.scrollView.frame.size;
    _panel = [[UIView alloc]initWithFrame:alertFrame];

    for (int i = 1; i < numberOfGraphs; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;
        UIView *subview = [[UIView alloc] initWithFrame:frame];
        subview.backgroundColor = [colors objectAtIndex:i];
        [self.scrollView addSubview:subview];
    }
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
    _scrollView.delegate = (id)self;
    self.alertsSubView.delegate = self;
}
-(AlertsView *)alertsSubView
{
    if(!_alertsSubView)
    {
        CGRect alertsViewFrame = CGRectMake(0,0, _panel.bounds.size.width,     _panel.bounds.size.height);
        _alertsSubView = [[AlertsView alloc] initWithFrame:alertsViewFrame];
        _alertsSubView.backgroundColor = [UIColor purpleColor];
        [self.scrollView addSubview:self.alertsSubView];
    }
    return _alertsSubView;
}
链接地址: http://www.djcxy.com/p/87764.html

上一篇: 自定义UIButton不会更改其图像

下一篇: 子视图不可见