Clearing/Resetting all string and variables - ios

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];
}

Related

isEqual is not working properly in xcode

My code looks like proper but isEqual used to compare two object is not working. i'm new to iOS. there is no much resource to check. [box2 isEqual:box1] is always gives me No. but it supposed to give Yes. anything wrong in my code, please suggest me the correct thing. thanks in advance. expecting best suggestion .
#import <Foundation/Foundation.h>
#interface Box:NSObject
{
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
}
#property(nonatomic, readwrite) double height; // Property
-(double) volume;
//-(id)initWithLength:(double)l andBreadth:(double)b;
#end
#implementation Box
#synthesize height;
-(id)init
{
self = [super init];
if(self)
{
length = 2.0;
breadth = 3.0;
}
return self;
}
-(double) volume
{
return length*breadth*height;
}
#end
#class Box;
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Box *box1 = [[Box alloc]init]; // Create box1 object of type Box
Box *box2 = [[Box alloc]init];
NSLog (#"hello world");
box1.height = 5.0;
NSLog (#"%ld",[box1 volume]);
if([box2 isEqual:box1])
{
NSLog(#"%b",[box2 isEqual:box1]);
}
[pool drain];
return 0;
}
You have to override isEqual method in your object.
e.g. in your "#implementation Box"
- (BOOL)isEqual:(id)object
{
if ([object isKindOfClass:[Box class]]) {
Box*box = (Box*)object;
return self.height == box.height; // compare heights values
}
return [super isEqual:object];
}
isEqual: works just fine and as documented, but not as you expected.
isEqual: is a property of NSObject. It returns YES if two object pointers are the same, and NO if they are not. If you want a different result, you need to override isEqual for your Box class. Note that the argument to isEqual: could be anything, not just a Box, so you need to be careful.
Obviously you shouldn't declare instance variables put properties, so the code below assumes that. The way your code is written, you will have some confusion with an instance variable height, a property height, and an instance variable _height, which will give you headaches. It also assumes that you are not subclassing Box (for example you could have a coloured box, which should not be equal to a Box with same width, height and breadth).
- (BOOL)isEqual:(id)other
{
if (! [other isKindOfClass:[Box class]]) return NO;
Box* otherBox = other;
return _length == other.length && _breadth == other.breadth
&& _height == other.height;
}

UILabel int count ++ doesn't work

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 {
...

Declare typedef struct Variable

I have this typedef in my .h file
typedef struct
{
NSInteger openTime;
NSInteger closeTime;
} ShopHours;
Now I want to declare it in my .m file and then like you would a NSString that you want to use throughout the .m.
SO basically I want to initialize it, as an array of the typedef and then so I can assign it a vaule in my viewDidLoad method, as I see fit.
So basically I want to declare it like
ShopHours weekSchedule[] = {};
And then in viewDidLoad
weekSchedule[] = {//my data}
But when I try to declare and use it like this I get compile error
Field has incomplete type 'SHopHours[]'
Thanks for the help in advance
The compiler needs to know how big the weekSchedule variable is in order to put some memory aside for it. You could declare it as shopHours weekSchedule[7] for example.
I tried, the problem was you need to decleare the size of the array first :
#interface LoginScreen (){
ShopHours aa[5];
}
- (void)viewDidLoad
{
[super viewDidLoad];
for (int i = 0; i < 5; i++) {
aa[i].closeTime = 5+i;
aa[i].openTime = 10 + i;
}
}
In another method I logged it:
for (int i = 0; i < 5; i++) {
NSLog(#"Open Time %ld\n",(long)aa[i].openTime);
NSLog(#"Close Time %ld\n",(long)aa[i].closeTime);
}
Works just fine.
If you want dynamic memory allocation,you can do it
#interface LoginScreen (){
ShopHours *aa;
}
- (void)viewDidLoad
{
[super viewDidLoad];
aa = malloc(sizeof(ShopHours) * 5);
for (int i = 0; i < 5; i++) {
aa[i].closeTime = 5+i;
aa[i].openTime = 10 + i;
}
}
Hope this helops.. :)

My UILabel is not displaying when my if statement is false

I have a story board that has a UITextField, UIButton, UIImage, and UILabel to display the images in an array. If you type the correct name for the image file into a text field. So, the problem is that once the text field input does not match, it should update the UILabel to display "Result not found", but it doesn't.
#import "ViewController.h"
#interface ViewController ()
{
myClass *myNewClass;
NSMutableArray *picArray;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
picArray = [#[#"Button_Red",#"Button_Green"]mutableCopy];
}
- (IBAction)displayImageAction:(id)sender
{
NSString *titleSearched = self.textSearchField.text;
NSString *titleNotHere = self.notFoundLabel.text;
//Declare a bool variable here and set
BOOL variable1;
for (int i = 0; i < picArray.count; i++)
{
NSString *currentPic = picArray[i];
if ([titleSearched isEqualToString:currentPic])
{
variable1 = YES;
}
}
if (variable1 == YES) {
//this works fine displays the image
self.outputImage.image = [UIImage imageNamed: titleSearched];
[self.textSearchField resignFirstResponder];
} else {
//problem is here its not showing when input for the array is not equal it should display a message label "Result Not Found" but it remains blank on the IOS simulator
titleNotHere = [NSString stringWithFormat:#"Result Not found"];
[self.textSearchField resignFirstResponder];
}
}
//Get rid of the texfield when done typing
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Retract keyboard if up
[self.textSearchField resignFirstResponder];
}
Your problem is that
titleNotHere = [NSString stringWithFormat:#"Result Not found"];
simply sets the method variable titleNotHere.
What you want is
self.notFoundLabel.text=#"Result Not found";
You will also want
self.notFoundLabel.text=#"";
when the result is found.
I think you will have to SET the value to the variable
self.notFoundLabel.text = [NSString stringWithFormat:#"Result Not found"]
or
self.notFoundLabel setText:[NSString stringWithFormat:#"Result Not found"]
UILabel.text = #"whatever..." is actually converted into [UILable setText:#"whatever..."].
NSString *labelText = UILabel.text has to be thought as NSString *labelText = [UILabel text];
This:
NSString *titleNotHere = self.notFoundLabel.text;
stores the text from the label into a variable, but updating that variable again will not change the label text - it only changes what that variable points to.
You need to explicitly update the label text:
self.notFoundLabel.text = #"Result Not found";
note also that this uses a string literal - you don't need to use a format string as you aren't adding any parameters to it.
Also, when checking booleans, don't use if (variable1 == YES) {, just use if (variable1) { (it's safer).

Decimal place not displaying correctly in Xcode?

I got the decimal working now but for some reason its displaying backwards. It's probably my lack of math skills but here is the code:
-(IBAction)buttonDigitPressed:(id)sender {
currentNumber = (float)[sender tag] + currentNumber /10;
priceOne.text = [NSString stringWithFormat:#"%.2f",currentNumber];
}
Now when I type on my custom keyboard in this order: 5-2-1, it displays the number backwards: 1.25. I want it to display 5.21. Any ideas?
In your #interface add a new instance variable
float currentMultiplier;
Set it in your (re-)initialisation routine, so:
currentMultiplier = 1.0;
and recode the routine you mention in the question as:
-(IBAction)buttonDigitPressed:(id)sender
{
currentNumber += (float)[sender tag]*currentMultiplier;
currentMultiplier *= 0.1;
priceOne.text = [NSString stringWithFormat:#"%.2f",currentNumber];
}
-(IBAction)buttonDigitPressed:(id)sender {
currentNumber = currentNumber*10 + (float)[sender tag];
calculatorScreen.text = [NSString stringWithFormat:#"%2f",currentNumber];
}

Resources