0

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

我的主视图控制器的 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];
}
4

1 回答 1

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;
}
于 2013-05-24T19:10:59.497 回答