如何在UIScrollView中设置多个按钮?

时间:2011-08-30 11:54:05

标签: iphone objective-c

如何在UIScrollView中设置多个按钮,以及如何为特定按钮索引设置单击操作?

4 个答案:

答案 0 :(得分:1)

UIScrollView *view = ..;
UIButton *button1 = ... ;
button1.tag = 1;
UIButton *button2 = ... ;
button1.tag = 2;
UIButton *button3 = ... ;
button1.tag = 3;
[view addSubview:button1];
[view addSubview:button2];
[view addSubview:button3];

在IBAction方法中检查按钮标签以识别点击的按钮。

答案 1 :(得分:1)

您可以在viewDidLoad

上调用此(initScrollView)方法
- (void)viewDidLoad { 

arrayScrollViewImages = [[NSArray alloc] initWithObjects:
     [[MyKeyValuePair alloc] initWithKey:@"imagename1" withValue:@"imagename1.png"],
     [[MyKeyValuePair alloc] initWithKey:@"imagename2" withValue:@"imagename2.png"],nil];


[self initScrollView];

}

此处arrayScrollViewImages是NSArray

- (void) initScrollView
 {

    int width = 10;
    for (int index = 0; index < [arrayScrollViewImages count]; index++) {
        MyKeyValuePair *pair = [arrayScrollViewImages objectAtIndex:index];
        UIImage *image = [UIImage imageNamed:pair.value];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(width, 2, 34, 34)];
        [button setBackgroundImage:image forState:UIControlStateNormal];
        [button setTag:index];
        [button addTarget:self action:@selector(scrollViewButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        if (index == 0) {
            [button setBackgroundColor:[UIColor colorWithRed:0.59 green:0.38 blue:0.21 alpha:1.00]];
        }

        [scrollView addSubview:button];
        width += 45;
    }

    scrollView.contentSize = CGSizeMake(width, 48);
    scrollView.contentOffset = CGPointMake(0, 0);
}


- (void) scrollViewButtonTapped:(id)sender {
    for (UIButton *b in scrollView.subviews) {
        if ([b isKindOfClass:[UIButton class]]) {
            [b setBackgroundColor:[UIColor clearColor]];
        }
    }

    UIButton *button = (UIButton *) sender;
    [button setBackgroundColor:[UIColor colorWithRed:0.59 green:0.38 blue:0.21 alpha:1.00]];
    MyKeyValuePair *pair = [arrayScrollViewImages objectAtIndex:button.tag];
    labelEspecialidad.text = pair.key;
}

这里MyKeyValuePair是一个类 * MyKeyValuePair.h *

@interface MyKeyValuePair : NSObject {
    NSString *key;
    NSString *value;
}

// Properties
@property (nonatomic, retain) NSString *key;
@property (nonatomic, retain) NSString *value;

@end

<强> MyKeyValuePair.m

@implementation MyKeyValuePair
@synthesize key, value;

- (id) initWithKey: (NSString *) _key withValue: (NSString *) _value {
    if (self = [super init]) {
        self.key = _key;
        self.value = _value;
    }

    return self;
}

- (void) dealloc {
    [super dealloc];
    [self.key release];
    [self.value release];
}

@end

答案 2 :(得分:0)

使用XCode Interface Builder

  • 添加按钮(从侧面对象面板中选择并拖放它们)
  • 为每个按钮创建IBAction(在所有者类.h中并在.m中实施)
  • 通过鼠标右键单击并拖动
  • 将按钮链接到操作

在youtube上查看一些视频教程,例如

阅读开发者指南

答案 3 :(得分:0)

您可以使用一系列按钮来实现此目的。为每个按钮分配一个唯一的标记ID,并以编程方式在UIScrollView上展开按钮。让UIScrollView收听Touch Up Inside事件,然后您可以在其中确定唯一标记ID单击了哪个按钮。