开发者

How do I choose between multiple UILabels by touch?

开发者 https://www.devze.com 2023-03-19 10:56 出处:网络
I have 2 UIButtons setup(+/-) when tapped, the number changes开发者_开发知识库 by one within aUILabel. What do I need to do to have multiple UILabels and when one is touched I can change its current v

I have 2 UIButtons setup(+/-) when tapped, the number changes开发者_开发知识库 by one within a UILabel. What do I need to do to have multiple UILabels and when one is touched I can change its current value with the UIButton(+/-)?


Have a variable that you use as the active label, then add a gesture recognizer to both the UIlabels to capture taps:

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

Then in your tapReceived method, swap out the active label

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

Then in the method that you capture clicks to the +/- button, write to currentLabel

Edit: A quick and dirty implementation of your problem. In interface builder I made 2 labels and a button and hooked them up. When you tap a label, it becomes the currentLabel and when you tap the button, whichever label you chose is incremented by 1. Hope it helps.

.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


I had to do something similar recently for a dynamically created menu in my app. This worked well:

In the menu creation method:

#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++;
}

. . . which calls this when tapped:

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

NOTE: This is the ARC-compliant way; add [itemLabel release] after adding to the subview if you're not using ARC.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号