Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Have a variable that you use as the active label, then add a gesture recognizer to both the UIlabels to capture taps:</p> <pre><code>label.userInteractionEnabled = YES; UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecieved:)]; [label addGestureRecognizer:tap]; [tap release]; //repeat for each additional label </code></pre> <p>Then in your tapReceived method, swap out the active label</p> <pre><code> -(void) tapRecieved:(UITapGestureRecognizer *)tap{ currentLabel = (UILabel *) tap.view; } </code></pre> <p>Then in the method that you capture clicks to the +/- button, write to currentLabel</p> <p><strong>Edit:</strong> 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.</p> <p>.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @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 </code></pre> <p>.m</p> <pre><code>#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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload