我有一个源代码,其中主屏幕有10个按钮(图像按钮),每个按钮链接到带有tableview项的sql数据库,在XIB中我选择每个按钮的哪个标签,1,2,3,4。
我想让这些按钮滚动,怎么做?
我尝试将视图转换为scrollview,然后我删除了图像,并在scrollview中创建了按钮,但现在我不知道如何将每个按钮链接到tableview中的标签?
以下是我使用的:@implementation mainViewController;
@synthesize master; @synthesize模型;
(无效)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]; [button 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];
答案 0 :(得分:0)
创建时为标签指定标签:
[buttonName setTag:1];
[buttonName addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];
然后使用:
-(void)aMethod:(UIButton *)button {
if ([button tag] == 1) {
...do something...
}
}
希望这有帮助。