1

UIView.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface UIView : UIResponder {
    IBOutlet UILabel *endLabel;
    IBOutlet UIButton *goButton;
    IBOutlet UITextField *textBox1;
    IBOutlet UITextField *textBox2;

    @property(nonatomic, retain) UILabel *endLabel;
    @property(nonatomic, retain) UIButton *goButton;
    @property(nonatomic, retain) UITextField *textBox1;
    @property(nonatomic, retain) UITextField *textBox2;
}
- (IBAction)goButtonClicked;
@end

UIView.m

#import "UIView.h"

@implementation UIView

@synthesize textBox1, goButton;
@synthesize textBox2, goButton;
@synthesize textBox1, endLabel;
@synthesize textBox2, endLabel;
@synthesize goButton, endLabel;

- (IBAction)goButtonClicked {

}

@end
4

2 回答 2

4

对s有点疯狂@synthesize,是吗?我确实相信您的主要问题是声明@property必须@interface.

我很惊讶编译器没有抛出格陵兰岛大小的红旗,尽管如此。

此外,您可能打算创建一个自定义子类UIView; 我会用MyView.

//MyView.m -- correct synthesize declaration
@synthesize textBox1, goButton, textBox2, endLabel;

//MyView.h -- correct interface declaration
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface MyView : UIView {
  IBOutlet UILabel *endLabel;
  IBOutlet UITextField *textBox1;
  IBOutlet UITextField *textBox2;
  IBOutlet UIButton *goButton;
}

@property(nonatomic, retain) UIButton *goButton;
@property(nonatomic, retain) UILabel *endLabel;
@property(nonatomic, retain) UITextField *textBox1;
@property(nonatomic, retain) UITextField *textBox2;

@end
于 2009-07-12T19:57:15.693 回答
0

第一个问题是你正在命名你的类 UIView,它已经存在于 UIKit 中。请参阅@Williham关于解决此问题的建议。

每个属性只需要一个@synthesize,当属性名称与实例变量名称匹配时,您只需要在 .m 文件中执行以下操作:

@synthesize endLabel;
@synthesize goButton;
@synthesize textBox1;
@synthesize textBox2;

此外,您可能会遇到让您的IBAction方法发挥作用的问题。要将方法用于目标-操作链接,它必须有一个返回类型IBAction(你有正确的)并接受一个id代表发送者的参数。规范方法签名如下所示:

- (IBAction) goButtonClicked:(id)sender;

实际上,我会推荐一个与调用它的按钮没有显式关联的方法名称,特别是因为可能有其他方法可以调用相同的操作。(例如,如果您正在编写桌面应用程序,等效键或菜单命令可以做同样的事情。)

于 2009-07-15T03:45:40.503 回答