views:

91

answers:

3

In my hello world application, i have a button and a text field set up. You enter your name in the text field then press the button, then you're supposed to see "Hello, [name]!". However all i get is "Hello, World!" (the default for when there's no string in the text box), even when i input a name. As requested, here's the file:

//
//  MyViewController.m
//  HelloWorld
//
//  Created by RCIX on 7/10/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "MyViewController.h"

@implementation MyViewController

@synthesize textField;
@synthesize label;
@synthesize string;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (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;
}

- (void)dealloc {
    [textField release];
    [label release];
    [string release];
    [super dealloc];
}
- (IBAction)changeGreeting:(id)sender {
    self.string = textField.text;
    NSString *nameString = self.string;
    if ([nameString length] == 0) {
     nameString = @"World";
    }
    NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@!", nameString];
    label.text = greeting;
    [greeting release];
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
     [theTextField resignFirstResponder];
    return YES;
}

@end
+1  A: 

Double-check your connections in Interface Builder. If textField isn't connected properly, then your method won't be able to read the text property of the field right, which may lead to its having a length of 0.

Tim
All of my connections appear to be connected properly, so i dont think that's the problem.
RCIX
It actually appears that i missed a connection. I didn't realize that i was supposed ot hook up the file owner's textField property to the actual text field.
RCIX
+1  A: 

There is no need to make nameString = string, just work on [self string] directly!

Here a working example:

string = textField.text;
if ([string length] == 0) {
    string = @"World";
}

NSString *greeting = [NSString stringWithFormat:@"Hello, %@!", string];
[label setStringValue:greeting];

You'll see I changed initWithFormat to stringWithFormat. If you use stringWithFormat, the NSString object is autoreleased, so we don't have to worry about releasing it.

One last thing... I don't really see the need for having "string" be an instance variable, as it is just used temporarily... it might be better to just go

NSString *string = textField.text;

and not have it declared in the @interface... just a thought.

micmoo
These are all ways to improve your code... but I guess it does nothing fundamentally different, so it doesn't really answer your question... so sorry. The code itself does work though, so I don't know what the problem is.
micmoo
A: 

Is the text field hooked up properly in Interface Builder?

dreamlax
Yep, it is (15 chars)
RCIX