标识符错误等

时间:2012-03-14 16:20:49

标签: xcode

我的代码中有一些错误,我已经注释掉但不知道如何修复。你能看看我告诉我需要做些什么来解决它吗?

TIA

实施档案

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize textField;
@synthesize label;

@synthesize userName = _userName;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setTextField:nil];
    [self setLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (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]; // Use of undeclared identifier 'nameString'
self.label.text = greeting; // Unknown type 'self'
} // Expected external declaration
@end

接口文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *label;

- (IBAction)changeGreeting:(id)sender;

@property (copy, nonatomic)NSString *userName;

@end

1 个答案:

答案 0 :(得分:1)

第一个文件中的最后一个if块没有左括号

...
- (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;
}
@end