UILabel int count ++ doesn't work - ios

I'm trying to indefinitely count up a number of taps on a UILabel, every time the label is tapped a different string is displayed. However, it stops at 2 taps always using ++ or += 1
-(void)cycleLabelString {
int taps;
taps += 1;
NSLog(#"taps = %d", taps);
if (taps == 1) {
self.randomLabel.text = [NSString stringWithFormat:#"$%.2f", pagesCount * 0.69];
} else if (taps == 2) {
self.randomLabel.text = [NSString stringWithFormat:#"%d", pagesCount];
} else if (taps >= 3) {
NSLog(#" >= 3");
}
}

int taps;
This initializes a new taps each time, and it is initialized to zero by default. You probably want it in a property. Make a private class extension at the top of your .m file like this:
#interface YourClassNameHere ()
#property (nonatomic) int taps;
#end
And then to use it:
-(void)cycleLabelString {
self.taps += 1;
NSLog(#"taps = %d", self.taps);
if (self.taps == 1) {
self.randomLabel.text = [NSString stringWithFormat:#"$%.2f", pagesCount * 0.69];
} else if (self.taps == 2) {
self.randomLabel.text = [NSString stringWithFormat:#"%d", pagesCount];
} else if (self.taps >= 3) {
NSLog(#" >= 3");
}
}

is this function getting called everytime the label is tapped? if so, you will need to define taps as a global variable, as it is being reset everytime the label is tapped. try something like:
int taps;
-(void)cycleLabelString {
...

Related

Cocos2d - CCLabelTTF overlays new value when updated

I have a CCLabelTTF variable defined as follows
#implementation LevelScene
{
CCLabelTTF *_collectedLabel;
int collectedStars1;
int collectedStars2;
int collectedStars3;
}
I also have a CCScrollView and I listen for scrollViewDidScroll.
- (void)setPageIndicatorTo:(NSInteger)page
{
if (page == 0) {
_collectedLabel.string = [NSString stringWithFormat:#"%d/75", collectedStars1];
} else if (page == 1) {
_collectedLabel.string = [NSString stringWithFormat:#"%d/75", collectedStars2];
} else if (page == 2) {
_collectedLabel.string = [NSString stringWithFormat:#"%d/75", collectedStars3];
}
}
- (void)scrollViewDidScroll:(CCScrollView *)scrollView
{
[self setPageIndicatorTo:_scrollView.horizontalPage];
}
In didLoadFromCCB, I set _collectedLabel.string to some value. It gives me the following :
When the view scroll, it calls - (void)setPageIndicatorTo:(NSInteger)page and thus updates the string. I then get
It should be 0/75. The new label overlays the previous one... How can I force the string to be updated?
I tried setting the string to "" before setting it to another value, but it gave the same result..

Objective-C, inheritance between functions

What I want is to inherit trueCount and falseCount values from func1 to func2 as you can see in the code below.
Any help on how to do that ?
- (void)func1 {
int trueCount = 0;
int falseCount = 0;
if (test3) {
trueCount++;
} else {
falseCount++;
}
if (trueCount >= falseCount) {
NSLog(#"TRUE WINS !!";
} else if (trueCount < falseCount) {
NSLog(#"False WINS !!");
}
}
- (void)func2 {
///HOW to make trueCount AND falseCount work here too, so that they inherit the same value in func1 ??
if (trueCount >= falseCount) {
NSLog(#"Show 1 !!";
} else if (trueCount < falseCount) {
NSLog(#"Show 2");
}
}
Your example does not show functions, it shows methods. Neither methods nor functions can be inherited - only classes can inherit other classes. However, nothing stops you from calling one method from inside another method to share the results produced by it. You could also move the shared functionality into a "helper" method, and use it in both places, like this:
// Define the shared method. Note the use of pointers.
-(void)countTrue:(int*)trueCount andFalse:(int*)falseCount {
*trueCount = 0;
*falseCount = 0;
if (test3) {
*trueCount++;
} else {
*falseCount++;
}
}
- (void)func1 {
int trueCount;
int falseCount;
// Invoke the shared functionality from the first place in your code
[self countTrue:&trueCount andFalse:&falseCount];
if (trueCount >= falseCount) {
NSLog(#"TRUE WINS !!";
} else if (trueCount < falseCount) {
NSLog(#"False WINS !!");
}
}
- (void)func2 {
int trueCount;
int falseCount;
// Invoke the shared functionality from a second place in your code
[self countTrue:&trueCount andFalse:&falseCount];
if (trueCount >= falseCount) {
NSLog(#"Show 1 !!";
} else if (trueCount < falseCount) {
NSLog(#"Show 2");
}
}
Now the logic of counting is placed in a single place, so you do not need to repeat it.
You'll want to do something like this in your header:
#inteface GameCounter
#property (nonatomic) NSInteger trueCount;
#property (nonatomic) NSInteger falseCount;
#end
and then use self.trueCount and self.falseCount
This question really has nothing to do with iOS or Objective-C. It is an Object Oriented question.
You have methods in a class. Those methods operate on some state, configuring, modifying and querying that state. Your methods (they aren't functions) are called without parameters, so no state is being added. Your class should have state of its own.
#interface MyClass ()
#property (nonatomic) NSInteger trueCount;
#property (nonatomic) NSInteger falseCount;
#property (nonatomic) BOOL test3;
#end
#implementation MyClass
- (void)method1 {
if (self.test3) {
self.trueCount++;
} else {
self.falseCount++;
}
if (self.trueCount >= self.falseCount) {
NSLog(#"TRUE WINS !!";
} else if (self.trueCount < self.falseCount) {
NSLog(#"False WINS !!");
}
}
- (void)method2 {
if (self.trueCount >= self.falseCount) {
NSLog(#"Show 1 !!";
} else {
NSLog(#"Show 2");
}
}
#end

Change Text Of A UILabel To A Random Array Object By Pressing UIButton

As the title says... I need to change the contents of my UILabel to a random object from NSArray by pressing a UIButton... Here is the code i have in the button:
- (IBAction)moodButton:(UILongPressGestureRecognizer *)sender {
UILabel *moodLabel = [[UILabel alloc] init];
if (sender.state == UIGestureRecognizerStateEnded) {
NSArray *moodArray = [[NSArray alloc] initWithObjects:#"Happy", #"Angry", #"Sad", #"Bored",
#"Tired", #"Stressed", #"Busy", nil];
id randomObject = [moodArray objectAtIndex:arc4random_uniform([moodArray count])];
if (randomObject == moodArray[0]) {
moodLabel.text = #"Happy";
}
else if (randomObject == moodArray[1]) {
moodLabel.text = #"Angry";
}
else if (randomObject == moodArray[2]) {
moodLabel.text = #"Sad";
}
else if (randomObject == moodArray[3]) {
moodLabel.text = #"Bored";
}
else if (randomObject == moodArray[4]) {
moodLabel.text = #"Tired";
}
else if (randomObject == moodArray[5]) {
moodLabel.text = #"Stressed";
}
else if (randomObject == moodArray[6]) {
moodLabel.text = #"Busy";
}
}
}
What am I doing wrong?
Thanks in advance.
You have a number of issues with your code.
You're creating a new label and then doing nothing with it other than adding some text (not even adding it to another view). Do you have an existing label you want to use instead? I'd recommend you keep a property pointing to a label in your view, which you can use to just update the text.
Your entire if statement is completely redundant. randomObject already contains a random string from your array, so you don't need to manually check which value it contains. Just remove your whole if statement, and do:
moodLabel.text = (NSString *)randomObject;
The answer by James covers the issues but I thought I would clarify by showing some code.
- (IBAction)moodButton:(UILongPressGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
NSArray *moodArray = #[ #"Happy", #"Angry", #"Sad", #"Bored",
#"Tired", #"Stressed", #"Busy" ];
NSString *randomString = moodArray[arc4random_uniform([moodArray count])];
self.moodLabel.text = randomString;
}
}
Note how the label is needs to be obtained from the existing label. Don't create a new one. Also note the use of modern Objective-C syntax for the array and its access.

Clearing/Resetting all string and variables

I have a routine in xcode that calls a function and spits out a result to a UITextView element on an iPhone app.
Before the routine is started I want to reset all NSString objects/values and any variables that I used to calculate the result. Is there a quick way to do this?
So far no matter what I retype in the UITextField it just gets ignored after the initial calculation.
Code for the solve button :-
- (IBAction)Solve:(id)sender {
//
// Reset strings and variable code here
//
[_Number1 resignFirstResponder];
[_Number2 resignFirstResponder];
[_Number3 resignFirstResponder];
[_Number4 resignFirstResponder];
[_Number5 resignFirstResponder];
[_Number6 resignFirstResponder];
[_TargetNum resignFirstResponder];
// Dismiss Keyboard
int numb1,numb2,numb3,numb4,numb5,numb6,numTar;
numb1 = [self.Number1.text intValue];
numb2 = [self.Number2.text intValue];
numb3 = [self.Number3.text intValue];
numb4 = [self.Number4.text intValue];
numb5 = [self.Number5.text intValue];
numb6 = [self.Number6.text intValue];
numTar = [self.TargetNum.text intValue];
mainLoopOne(numb1,numb2,numb3,numb4,numb5,numb6,numTar);
// Start calculation with field values
readAnswers = #"Please read answers bottom-up.\n";
cantCalc = NULL;
finalResult = #"";
if (numb1 != 0) {
int ii;
for(ii = 0; ii < 6 ; ii++) {
if (allAnswers[ii] != NULL) {
finalResult = [finalResult stringByAppendingFormat:#"%#", allAnswers[ii]];
}
}
if (finalResult == NULL) {
cantCalc = #"Unfortunately, that doesn't seem to be possible, as there was no answer calculated.\n";
readAnswers = #"";
}
CompileText = [NSString stringWithFormat:#"%#\n%#\nfrom %d combination tries.", readAnswers, finalResult, countCombi];
[self.TextWin setText:CompileText];
}
countCombi = 0;
}
#end
Most of the strings and variables are set just under the #import "ViewController.h" so they are global :-
NSString *readAnswers;
NSString *cantCalc;
NSString *finalResult;
NSString *allAnswers[10];
NSString *CompileText;
NSString *readAnswers;
NSString *finalResult;
#define DIV 0
#define MUL 1
#define ADD 2
#define SUB 3
int n1,n2,n3,n4,n5,n6;
int answer_counter = 0;
int tar2 = 0;
int number[6];
int target = 0;
int used[6];
int countCombi;
Thanks.
Create a reset method that alloc inits every string in your view controller again. You can set the textview's text to "".
In theory you could write a helper class that you pass in an object (your view controller) and it uses introspection to reset everything that is a string or whatever you want. This is a good approach for modularity and reusability, however it requires knowledge of c, and introspection.
Yes whatever williams say thats correct. Implement like below:
-(IBAction)doClear:sender
{
[Yourtextview setString];
}

Disable a gesture from firing

I have a tap gesture on my view that I only want to be able to fire after a certain point and after a different function has run. My solution was to create a outlet for the tap gesture and then call the Enabled function setting to TRUE or FALSE as needed. The code looks like this, with the second function being the tap gestures
- (IBAction)butt:(id)sender
{
if(game == FALSE)
{
tapges.enabled = FALSE;
int sec;
sec = arc4random() % 5;
if(sec == 0){sec++;}
sleep(sec);
tapges.enabled = TRUE;
game = TRUE;
gun.hidden = FALSE;
start = [[NSDate date] timeIntervalSince1970];
}
}
- (IBAction)tap:(id)sender
{
if(game == TRUE)
{
end = [[NSDate date] timeIntervalSince1970];
double total = end - start;
NSString *myString = [NSString stringWithFormat:#"%lf", total];
ss.text = myString;
game = FALSE;
gun.hidden = TRUE;
tapges.enabled = FALSE;
}
}
However it doesn't seem to work. Even after the first function has set tapges.enabled = FALSE if you tap before it reaches tapges.enabled = TRUE it still just queues the tap function for running after the butt function runs.
There is a way to prevent a UIGestureRecognizer from firing (or to call another function before it fires) through using UIGestureRecognizerDelegate method gestureRecognizerShouldBegin but I think the issue is more a question of logic.
Not sure I entirely understand your task but wouldn't something more simple like:
- (IBAction)butt:(id)sender
{
if(game == FALSE)
{
[self callOtherMethod];
}
}
Or
- (IBAction)butt:(id)sender
{
if(game == FALSE)
{
//do set up
} else {
// do other setup
}
}

Resources