how to set multiple buttons in UIScrollView?
how to set multiple buttons in UIScrollView开发者_StackOverflow中文版 and also how to set clicked action for particular button index?
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];
In IBAction method check button tag to identify clicked button.
you can call this(initScrollView) method on viewDidLoad
- (void)viewDidLoad {
arrayScrollViewImages = [[NSArray alloc] initWithObjects:
[[MyKeyValuePair alloc] initWithKey:@"imagename1" withValue:@"imagename1.png"],
[[MyKeyValuePair alloc] initWithKey:@"imagename2" withValue:@"imagename2.png"],nil];
[self initScrollView];
}
here arrayScrollViewImages is 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;
}
here MyKeyValuePair is a one class *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
Using XCode Interface Builder
- Add buttons (select and drag&drop them from side objects panel)
- Create
IBAction
for every button (in owner class.h
and implement in.m
) - Link buttons to actions via right mouse click&drag
See some video tutorials on youtube, such as
- http://www.youtube.com/watch?v=WgAAvRQBrPc
- http://www.youtube.com/watch?v=uAdmetwz7sc
- http://www.youtube.com/watch?v=vixD-N6cbDY
and read Developer Guide
- http://www.switchonthecode.com/tutorials/creating-your-first-iphone-application-with-interface-builder
- http://developer.apple.com/library/ios/#documentation/IDEs/Conceptual/Xcode4TransitionGuide/InterfaceBuilder/InterfaceBuilder.html
You can use an array of buttons for that purpose. Assign a unique tag id to each button, and programmatically spread the buttons along the UIScrollView. Have the UIScrollView listen to the Touch Up Inside event, where you can then determine which button has been clicked by the unique tag ID.
精彩评论