PDA

View Full Version : Starting out, hit a problem




ethical
Nov 24, 2009, 03:43 PM
Hi guys,

I've started trying to learn to programme iPhone apps, I'm not very far along (in fact I'm right at the begining), but have got a problem with a little bit of code I've been playing around with.

The aim of it is basically a calculator, but with none of the calculator function (:D). All I wanted to do was be able to click the numbers (on the simulator) and see them put into the label one after the other (as you would on a calculator, to type in a number).

Anyway, the problem I get is that when I click a number, a whole bunch of random numbers pop up. If I click another number they change to another random bunch, if I click the first number again they switch back to the first set.

First attachment is straight after launch. Second is after click "2". I put a space between the numbers (initial 0 and 2) to make it easier to see. The first set of numbers do not change, but the second set changes depending on what is clicked. The new number clicked doesnt even get added onto the end like it should, it just replaces the numbers already there. Hopefully this makes sense!

But enough rambling, here's the code (interface followed by implementation).

#import <UIKit/UIKit.h>

@interface Calculator_iPhoneViewController : UIViewController {
UILabel *numberUpdate;

}

@property (nonatomic retain) IBOutlet UILabel *numberUpdate;

- (IBAction) buttonPressed: (id) sender;
@end



#import "Calculator_iPhoneViewController.h"

@implementation Calculator_iPhoneViewController
@synthesize numberUpdate;

- (IBAction) buttonPressed: (id) sender
{
NSString *title = [sender titleForState:UIControlStateNormal];
NSString *newNumber = [[NSString alloc] initWithFormat:
@"%i %i", numberUpdate, title];

numberUpdate.text = newNumber;

[newNumber release];
}
- (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.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.numberUpdate = nil;
}
- (void)dealloc {
[numberUpdate release];
[super dealloc];
}
@end



oculus42
Nov 24, 2009, 03:59 PM
I've not programmed at all for the iPhone or in Objective-C but those look like pointer addresses to me. I can't decipher the code well enough, but my guess is you are retrieving the button's address rather than following the reference to the button somewhere along the way.

dejo
Nov 24, 2009, 04:02 PM
Here's your problem:
NSString *title = [sender titleForState:UIControlStateNormal];
NSString *newNumber = [[NSString alloc] initWithFormat:
@"%i %i", numberUpdate, title];

title is an NSString not an int, which is how your trying to handle in in the initWithFormat: use of "%i".

ethical
Nov 24, 2009, 04:24 PM
Here's your problem:

title is an NSString not an int, which is how your trying to handle in in the initWithFormat: use of "%i".

ah i see. thank you.

when i change the title %i to %@ it solves the second set of random numbers, but what should be a zero still changes to a random set of numbers. if i change the %i for numberUpdate to %@ as well then i get an error in the label.

Also, it doesnt add one number after another like it should. it only changes the second number.

what i want it to do is this:
assume numberUpdate starts as 0.
click 3 --> newNumber is 03. numberUpdate becomes 03
click 4 --> newNumber is numberUpdate followed by title (which is 4) so newNumber is 034. numberUpdate become 034
and so forth.

i've probably made this way more complicated then it needs to be.

dejo
Nov 24, 2009, 04:41 PM
Store your "accumulator" as a number, perhaps NSNumber or int. Then when you get a new digit, multiply the existing value by 10 and add the new digit. To display, convert the number to a string. Also, don't assume any value is already assigned. Assign it yourself, as in: int accumulator = 0;

ethical
Nov 24, 2009, 05:55 PM
Store your "accumulator" as a number, perhaps NSNumber or int. Then when you get a new digit, multiply the existing value by 10 and add the new digit. To display, convert the number to a string. Also, don't assume any value is already assigned. Assign it yourself, as in: int accumulator = 0;

ok thanks! i'll see if i can work that out. much appreciated!