ScrollView无法以编程方式工作

时间:2014-10-28 05:27:36

标签: ios objective-c iphone uiscrollview

Jest我尝试以编程方式ScrollView。它在ViewController.m中运行良好 我试过这个

scroll=[[UIScrollView alloc]init];
    scroll.frame=CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height);
    scroll.delegate=self;
    scroll.scrollEnabled=YES;
    scroll.contentSize =CGSizeMake(100, 525);
    scroll.backgroundColor=[UIColor yellowColor];
    [self.view addSubview:scroll];

    goo=[[UIButton alloc]init];
    goo.frame=CGRectMake(0, 50, 100, 100);
    goo.backgroundColor= [UIColor brownColor];
    [goo setTitle:@"good Day" forState:UIControlStateNormal];
    [goo addTarget:self action:@selector(ok:) forControlEvents:UIControlEventTouchDown];
    [scroll addSubview:goo];

工作不错,但相同的代码无法在SecondViewControl

中使用
-(void)ok:(id)sender
{
     Second *second=[[Second alloc]initWithNibName:nil bundle:nil];
     [second setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentViewController:second animated:YES completion:nil];

}

Second.m

scroll=[[UIScrollView alloc]init];
        scroll.frame=CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height);
        scroll.delegate=self;
        scroll.scrollEnabled=YES;
        scroll.contentSize =CGSizeMake(100, 525);
        scroll.backgroundColor=[UIColor yellowColor];
        [self.view addSubview:scroll];

我不明白为什么scrollView无法在Second.m中工作,但同样的代码工作在ViewControl.m.我的Xcode Version 6.0.1

2 个答案:

答案 0 :(得分:1)

请使用presentViewController代替modalViewController,并将UIScrollView的代表设为SecondViewController

答案 1 :(得分:0)

检查以下步骤:

  1. 你是否符合你的第二堂课UIScrollViewDelegate

  2. 如果未设置,代码scroll.delegate=self;将导致问题。