滚动视图外的按钮

时间:2013-11-16 01:10:45

标签: ios objective-c scrollview

我只在屏幕的一部分设置了scrollView。

    UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];
    scrollView.contentSize=CGSizeMake(320,1500);//568    
    [self.view addSubview:scrollView];

另外,在scrollView中我设置了一个按钮。

    UIButton *hintbtn = [[UIButton alloc] initWithFrame:CGRectMake(160,self.view.frame.size.height - 50,160,50)];
    [hintbtn addTarget:self action:@selector(hint:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:hintbtn];

如果按此按钮,我想让scrollView向下滚动。

    -(void)hintbtn:(UIButton*)button {
        [scrollView setContentOffset:CGPointMake(0, self.view.frame.size.height - 50) animated:YES];
    }

然而,它给出了一个错误,说“未知接收器'scrollView'”。狐狸有这种方法吗? 请帮帮我,如何处理这个问题。

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为您没有对scrollview变量的引用。要解决此问题,请执行以下操作:

在您的班级标题文件(* .h文件)中添加以下内容:

@interface YOUR_CLASS_NAME : UIViewController

// Add the following line to the header file
@property (nonatomic, strong) UIScrollView *scrollView;

@end

然后,在实现文件(* .m文件)中添加以下内容:

// Wherever you have this code. It's probably in 'viewDidLoad'
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];
self.scrollView.contentSize=CGSizeMake(320,1500);//568 
[self.view addSubview:self.scrollView];

// In the following line you have a typo. The selector should be "hintbtn:"!
[hintbtn addTarget:self action:@selector(hintbtn:) forControlEvents:UIControlEventTouchUpInside];

// Also modify this code:
-(void)hintbtn:(UIButton*)button {
    [self.scrollView setContentOffset:CGPointMake(0, self.view.frame.size.height - 50) animated:YES];
}

如果您有更多问题,请与我们联系。

希望这有帮助!

相关问题