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).
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
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).
Code:
#import <UIKit/UIKit.h>
@interface Calculator_iPhoneViewController : UIViewController {
UILabel *numberUpdate;
}
@property (nonatomic retain) IBOutlet UILabel *numberUpdate;
- (IBAction) buttonPressed: (id) sender;
@end
Code:
#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