Hi, this is a total newbie question and I've already resigned myself to the fact that I'm making some fundamental error.
Anyway, I'm making a simple app just to acclimate myself with Obj-C and iPhone. It has buttons for each of the 10 numbers. When the buttons are touched up they invoke the numPressed method. This method reads the tag of the button and puts it into an NSString (myS). I try to append the NSString to the NSMutableString newString, which is then outputted to a UILabel.
The problem I'm running into is that newString doesn't appear to have anything in it when it is displayed. If I use myS instead of newString it is displayed properly. The desired behavior is for the label to display the characters the user inputs following one another.
When creating a new object, alloc will be first, then init. And the alloc method will pretty much always be sent to a class, not a variable.
Also, this doesn't work. You are sending a message to newString, but newString doesn't exist yet.
Thank you both for the very quick responses. That was a little embarrassing but like I said, I figured there was at least one fundamental error on my part.
Anyway, I was still having difficulty getting the NSMutableString to behave like I desired so this is what I did instead:
Code:
- (IBAction) numPressed: (id) sender
{
//myS is an instance variable now, newNum is the most recently
//pressed button, newNum is added to myS and sent to the label
NSString *newNum = [NSString stringWithFormat:@"%i", [sender tag]];
if (myS == nil)
myS = newNum;
else
myS = [myS stringByAppendingString:newNum];
hey.text = myS;
}
This does what I was trying to do (simply display numbers the user enters... lol). It doesn't use the NSMutableString class at all, but I don't like that I wrote this because I couldn't get mutable objects to work for me...
Thank you both for the very quick responses. That was a little embarrassing but like I said, I figured there was at least one fundamental error on my part.
Anyway, I was still having difficulty getting the NSMutableString to behave like I desired so this is what I did instead:
Code:
- (IBAction) numPressed: (id) sender
{
//myS is an instance variable now, newNum is the most recently
//pressed button, newNum is added to myS and sent to the label
NSString *newNum = [NSString stringWithFormat:@"%i", [sender tag]];
if (myS == nil)
myS = newNum;
else
myS = [myS stringByAppendingString:newNum];
hey.text = myS;
}
This does what I was trying to do (simply display numbers the user enters... lol). It doesn't use the NSMutableString class at all, but I don't like that I wrote this because I couldn't get mutable objects to work for me...
Why not post the mutable string code that isn't working?
Once you fix your [[NSMutableString alloc] initWithCapacity: x] problem, the rest of the code looked like it would work.
Show us the way you defined your properties too. The way you are assigning those properties needs some cleanup. You need to learn good memory management habits.
Why not post the mutable string code that isn't working?
Once you fix your [[NSMutableString alloc] initWithCapacity: x] problem, the rest of the code looked like it would work.
Show us the way you defined your properties too. The way you are assigning those properties needs some cleanup. You need to learn good memory management habits.