子视图不可见

我用UIScrollView创建了一个UIViewController。 在scrollView我有一个页面控件。 当我测试它时,我能够水平滚动浏览四个不同的彩色页面。 现在我需要添加我需要的实际视图。 在项目的不同部分,我使用coreplot创建了一个线图。 现在我添加了一个新的UIview并添加了线图的代码。 我现在试图用linegraph UIView替换scrollView的第一页。 我没有得到任何错误,但屏幕上也没有出现任何错误。 它只是scrollView的默认背景。 scrollView仍然正确地滚动浏览其他页面。 我通过编程方式向UIView添加了一个按钮,以查看它是否与coreplot有关,但是我看不到这一点。 这是我的一些代码。 我将不胜感激任何建议。 谢谢。

我的主viewController的viewDidLoad方法: - (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);
}

这是我的UIView的initWithFrame方法的代码。 所有的NSLog语句都可以运行,所以我知道所有的东西都被调用了:

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

如果它们是相关的,我还会添加方法initPlot和ConfigureHost:

-(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];
}

我终于明白了这一点。 感谢您的建议@Eric Skroch。 实际上,这使我走上了正确答案的道路,即使这不是问题。 基本上要解决这个问题,我必须将所有的逻辑移入视图控制器,并改变代码的顺序。 我在这里发布修改过的视图控制器的相关部分以防万一任何人在类似的情况下运行。

-(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/87763.html

上一篇: subview not visible

下一篇: The SubView's background is lost