0

我有一个源代码,其中主屏幕有 10 个按钮(图像按钮),每个按钮都通过 tableview 项目链接到 sql 数据库,在 XIB 中我选择每个按钮的标签,1、2、3、4..

我想让这些按钮滚动,怎么做?

我试图将视图转换为滚动视图,然后我删除了图像,并在滚动视图中创建了按钮,但现在我不知道如何将每个按钮链接到它在 tableview 中的标签?

以下是我使用的:

@实现主视图控制器;

@合成大师;@synthesize 模型;

  • (void)viewWillAppear:(BOOL)动画{

    UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)]; scrollview.showsVerticalScrollIndicator=YES; scrollview.scrollEnabled=YES; scrollview.userInteractionEnabled=YES;[self.view addSubview:scrollview]; scrollview.contentSize = CGSizeMake(320,960);//你可以用你的要求来编辑它 scrollview.pagingEnabled=YES;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [按钮 addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown]; [button setTitle:@"Show View" forState:UIControlStateNormal]; button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);

    UIImageView* imageview1 =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"c1@2x.png"]]; imageview1.frame=CGRectMake(18.0, 18, 285,50); [scrollview addSubview:imageview1];

    UIImageView* imageview2 =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"c2@2x.png"]]; imageview2.frame=CGRectMake(18.0, 78, 285,50); [scrollview addSubview:imageview2];

    UIImageView* imageview3 =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"c3@2x.png"]]; imageview3.frame=CGRectMake(18.0, 138, 285,50); [scrollview addSubview:imageview3];

    UIImageView* imageview4 =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"c4@2x.png"]]; imageview4.frame=CGRectMake(18.0, 198, 285,50); [scrollview addSubview:imageview4];

4

1 回答 1

0

创建按钮时为其分配标签:

[buttonName setTag:1];
[buttonName addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];

然后使用:

-(void)aMethod:(UIButton *)button {
 if ([button tag] == 1) {
  ...do something...
 }
}

希望这可以帮助。

于 2014-01-15T11:01:27.933 回答