Xcode - 子视图不可见

时间:2013-05-23 16:53:51

标签: ios xcode uiscrollview core-plot subview

我创建了一个带有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];
}

1 个答案:

答案 0 :(得分:0)

我终于弄明白了。感谢您的建议@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;
}