如何通过触摸在多个UILabel之间进行选择?

时间:2011-07-11 02:10:28

标签: iphone iphone-sdk-3.0

点击时我有2个UIButtons设置(+/-),UILabel中的数字改变一个。我需要做什么来拥有多个UILabel,当触摸一个UILabel时,我可以用UIButton(+/-)改变它的当前值?

2 个答案:

答案 0 :(得分:1)

拥有一个用作活动标签的变量,然后在两个UIlabels中添加一个手势识别器来捕获水龙头:

label.userInteractionEnabled = YES;
 UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecieved:)]; 
[label addGestureRecognizer:tap];
[tap release];
//repeat for each additional label

然后在tapReceived方法中,换出活动标签

 -(void) tapRecieved:(UITapGestureRecognizer *)tap{  
      currentLabel = (UILabel *) tap.view;
 }

然后在您点击+/-按钮的方法中,写入currentLabel

修改:快速而肮脏地解决您的问题。在界面构建器中,我制作了2个标签和一个按钮并将它们连接起来。当您点击标签时,它将成为当前标签,当您点按该按钮时,您选择的任何标签都会增加1.希望它有所帮助。

·H

#import <UIKit/UIKit.h>

@interface junkViewController : UIViewController {

    UILabel *label;
    UILabel *label2;
    UILabel *currentLabel;
    int label1Value;
    int label2Value;
}
@property (nonatomic, retain) IBOutlet UILabel *label;

@property (nonatomic, retain) IBOutlet UILabel *label2;
- (IBAction)buttonTap:(id)sender;
@end

的.m

#import "junkViewController.h"

@implementation junkViewController
@synthesize label;
@synthesize label2;

- (void)dealloc
{
    [label release];
    [label2 release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/
-(void) tapRecieved:(UITapGestureRecognizer *)tap{
    currentLabel = (UILabel *)tap.view;
    NSLog(@"tap %@",tap.view);
}
-(void) viewDidLoad{
    currentLabel = label;
    label.text = [NSString stringWithFormat:@"%d",label1Value];
    label2.text = [NSString stringWithFormat:@"%d",label2Value];
    label.userInteractionEnabled = YES;
    UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecieved:)]; 
    [label addGestureRecognizer:tap];
    [tap release];
    tap = nil;
    tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecieved:)]; 
    label2.userInteractionEnabled = YES;
    [label2 addGestureRecognizer:tap];
    [tap release];


}
- (void)viewDidUnload
{
    [self setLabel:nil];
    [self setLabel2:nil];
    [super viewDidUnload];
       // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)buttonTap:(id)sender {
    int value = [currentLabel.text intValue] + 1;
    currentLabel.text = [NSString stringWithFormat:@"%d",value];
}
@end

答案 1 :(得分:0)

我最近在我的应用中为动态创建的菜单做了类似的事情。这很有效:

在菜单创建方法中:

#define NAV_WIDTH               220
#define NAV_HEIGHT              220
#define NAV_TOP_HEIGHT          16
#define NAV_ITEM_HEIGHT         30
#define NAV_BOTTOM_HEIGHT       24
#define NAV_LABEL_MARGIN        20

[navPage setFrame: CGRectMake(navLeftCorner, navTopCorner, NAV_WIDTH, NAV_HEIGHT)];
NSArray *menuTitles = [[NSArray alloc] initWithObjects: @"One", @"Two", @"Three", @"Four", nil];
int tag = 1;
double labelTop = NAV_TOP_HEIGHT;

for (NSString *title in menuTitles) {
    UILabel *itemLabel = [[UILabel alloc] initWithFrame: CGRectMake(NAV_LABEL_MARGIN, labelTop, NAV_WIDTH - (NAV_LABEL_MARGIN * 2), NAV_ITEM_HEIGHT)];
    [itemLabel setUserInteractionEnabled: YES];
    UITapGestureRecognizer *menuTap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(menuTapped:)];
    [itemLabel addGestureRecognizer: menuTap];
    [itemLabel setText: title];
    [itemLabel setTag: tag];
    [navPage addSubview: itemLabel];
    labelTop += NAV_ITEM_HEIGHT;
    tag++;
}

。 。 。点击时调用它:

- (void) menuTapped: (UITapGestureRecognizer *)tap
{
    UILabel *currentLabel = (UILabel *) tap.view;
    int index = (currentLabel.tag - 1);
    /* Do stuff based on the tag index */
}

注意:这是符合ARC的方式;如果您没有使用ARC,请在添加到子视图后添加[itemLabel release]。