I have just started developing for iPhone (I’ve been an Android developer for quite some time) and am having a problem with a very very…mundanely simple task: changing the text of a text label. I followed the Apple developer tutorial, but there is a problem with the initWithFormat method of NSString.
The issue is in this method:
-(IBAction)changeGreeting:(id)sender {
self.userName = self.textField.text;
NSString *nameString = self.userName;
if ([nameString length] == 0) {
nameString = @"World";
}
NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@!", nameString];
self.label.text = greeting;
}
When I run the project, whether there is a value in textField or not, the label is always set to Hello,…
When I change the last line of the method to:
self.label.text = self.userName;
or to:
self.label.text = nameString;
The label displays the value that SHOULD be inserted at the %@ in the initWithFormat call. Note: I have also tried using stringWithFormat and I get the same problem.
Thanks.
Your label is too narrow. Assuming that it’s in IB, open IB and make the label wider.