3

使用连接器连接按钮、文本字段和标签后,@synthesize 语句不会自动为标签和文本字段(两者都是出口)生成,因此当我尝试访问标签或文本字段或其在 . m 文件中显示“使用未声明的标识符”,但 Xcode 不应该自动执行此操作吗?我正在关注本教程http://www.youtube.com/watch?v=c3Yd2kCPs5c(大约在 4:35 显示自动生成的 @synthesize 语句,这些语句在 xcode 中不再发生),这就是我猜的原因. 我不应该手动添加这些对吗?解决此问题的最佳方法是什么?

-------.m Fie--------
//
//  ViewController.m
//  AutoConnection
//
//  Created by Administrator on 29/03/13.
//  Copyright (c) 2013 Administrator. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)changeLabel:(id)sender {

    NSString *message = [[NSString alloc] initWithFormat:@"Hello %@", [myTextFeild text]];
    [myLabel setText:message];

}
@end

---------.h file------------
//
//  ViewController.h
//  AutoConnection
//
//  Created by Administrator on 29/03/13.
//  Copyright (c) 2013 Administrator. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
- (IBAction)changeLabel:(id)sender;

@property (weak, nonatomic) IBOutlet UILabel *myLabel;

@property (weak, nonatomic) IBOutlet UITextField *myTextFeild;

@end
4

4 回答 4

5

看到你的代码后,

@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@property (weak, nonatomic) IBOutlet UITextField *myTextFeild;

您正在访问它们:

 [myLabel setText:message];

这是不正确的。

它应该是[_myLabel setText:message];

or [self.myLabel setText:message];

Reason: The compiler running with XCode4.4 and above, autosynthesizes your property as

@synthesize yourProperty=_yourProperty;

Or you can override this by, if you wish to use same property name

@synthesize yourProperty;
于 2013-03-30T05:09:39.730 回答
3

您不再需要使用@synthesize. 现在更容易了,因为您所要做的就是_在属性之前添加一个字符。例子

//header
@property (nonatomic) UILabel *l;

//implementation
-(void)viewDidLoad{
_l = [[UILabel alloc] init];
//do stuff
}

您也可以手动添加合成线

于 2013-03-30T05:07:53.667 回答
2

您需要使用 self 进行自动合成的属性使用

- (IBAction)changeLabel:(id)sender {

    NSString *message = [[NSString alloc] initWithFormat:@"Hello %@", [myTextFeild text]];
    [self.myLabel setText:message];

}

如果您有一个名为myLabel的属性,编译器将生成代码,就像

@synthesize myLabel=_myLabel;

在 Xcode 4.4 和 LLVM Compiler 4.0 中,不再需要 @synthesize 指令,因为它将默认提供。这意味着在大多数情况下,您现在只需要 @property 并且编译器会为您处理其他所有事情。一般来说,编译器会自动生成实例变量。

因此,如果您想访问可以使用的属性self.myLabel,例如使用情况,您可以使用_myLabel

于 2013-03-30T05:09:06.990 回答
2

The @synthesize is now done automatically by the compiler. The @property creates a getter and setter method which can be accessed using dot-notation (e.g. self.myLabel). These methods use a variable to store the object you are accessing. Xcode generates the variable name by adding an underscore to the property name.

Just use getters and setters and don't access an object using the instance variable (e.g. _myLabel = foo). That's bad style.

Here's a good iTunes U course covering all this: Coding Together: Developing Apps for iPhone and iPad

于 2013-03-30T05:23:48.830 回答