Help with CountMeIn: add 1 to UILabel2 if UILabel1 value is equal to UITextField
Im an absolute beginner and not English speacking..., I'm trying to play and modify the code of CountMeIn that you can find on http://appsamuck.com/day5.html. I've implemented more counters, (score1, score2, set1, set2) and a field for "race to:" what I would like to:
"race to" can be set to any value; i would like that when score1 is equal or more (+=) than "race to" it reset score1 and score2, and add 1 to set1. Then the same for score2... here is my code:
MainView.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface MainView : UIView {
IBOutlet UILabel *scorer;
IBOutlet UILabel *seter;
IBOutlet UILabel *scorer1;
IBOutlet UILabel *seter1;
IBOutlet UITextField *racer; // "race to"
}
- (IBAction)doneButtonOnKeyboardPressed: (id)sender;
- (IBAction)addUnit;
- (IBAction)subtractUnit;
- (IBAction)addUnit2;
- (IBAction)subtractUnit2;
- (IBAction)addUnit3;
- (IBAction)subtractUnit3;
- (IBAction)addUnit4;
- (IBAction)subtractUnit4;
- (IBAction??)countset; // ->?
- (IBAction??)countset1; // ->?
@end
MainView.m
#import "MainView.h"
@implementation MainView
int score = 00;
int set = 00;
int score1 = 00;
int set1 = 00;
int race = 00;
-(void)awakeFromNib; {
scorer.text = @"00";
seter.text = @"00";
scorer1.text = @"00";
seter1.text = @"00";
racer.text = @"00";
}
- (IBAction)addUnit {
if(score >= 99) return;
NSString *numValue = [[NSString alloc] initWithFormat:@"%02d", ++score];
scorer.text = numValue;
[numValue release];
if(scorer.text == racer.text) return;
NSString *numValue1 = [[NSString alloc] initWithFormat:@"%02d", ++set];
seter.text = numValue1;
[numValue1 release];
}
- (IBAction)subtractUnit {
if(score <= 00) return;
NSString *numValue = [[NSString alloc] initWithFormat:@"%02d", --score];
scorer.text = numValue;
[numValue release];
}
- (IBAction)addUnit2 {
if(set >= 99) return;
NSString *numValue = [[NSString alloc] initWithFormat:@"%02d", ++set];
seter.text = numValue;
[numValue release];
}
- (IBAction)subtractUnit2 {
if(set <= 00) return;
NSString *numValue = [[NSString alloc] initWithFormat:@"%02d", --set];
seter.text = numValue;
[numValue release];
}
- (IBAction)addUnit3 {
if(score1 >= 99) return;
NSString *numValue = [[NSString alloc] initWithFormat:@"%02d", ++score1];
scorer1.text = numValue;
[numValue release];
}
- (IBAction)subtractUnit3 {
if(score1 <= 00) return;
NSString *numValue = [[NSString alloc] initWithFormat:@"%02d", --score1];
scorer1.text = numValue;
[numValue release];
}
- (IBAction)addUnit4 {
if(set1 >= 99) return;
NSString *numValue = [[NSString alloc] initWithFormat:@"%02d", ++set1];
seter1.text = numValue;
[numValue release];
}
- (IBAction)subtractUnit4 {开发者_StackOverflow
if(set1 <= 00) return;
NSString *numValue = [[NSString alloc] initWithFormat:@"%02d", --set1];
seter1.text = numValue;
[numValue release];
}
- (IBAction)countset{ // I guess its no IBAction...
if(scorer.text += racer.text add 1 to setter.text // ->? i can't figure out...
Can someone help me please? There is for sure another way to write this with less code, but Ill find out later, I'm glad that its working so far. Thanks for any help, and sorry for my english.
If I correctly understand your question then here is a solution:
- (IBAction)countset
{
// compare values
if ([scorer.text intValue] >= [racer.text intValue])
{
// add 1
setter.text = [NSString stringWithFormat:@"%02d", [setter.text intValue] + 1];
// reset values
score1.text = @"";
}
}
so i did test and make adjustment, but its not fully working yet;
'countset' should not be an action since it had to check every time the score change, so the best way is to add it to the action 'addUnit':
- (IBAction)addUnit {
if(score >= 99) return;
NSString *numValue = [[NSString alloc] initWithFormat:@"%02d", ++score];
scorer.text = numValue;
[numValue release];
// compare values
if ([scorer.text intValue] >= [racer1.text intValue])
{
// add 1
seter.text = [NSString stringWithFormat:@"%02d", [seter.text intValue] + 1];
// reset values
scorer.text = @"00";
}
this is working, for the first set, but then I guess there is something missing, a release probably, Now it works, but after the first set is scored, the score is reset, but it add one set on every new point... Also, if there was a mistake I can't subtract unit to the set, I first have to add one before I can subtract...
hope you understand, and sorry if I shouldn't answer my own question.
精彩评论