iPhone:为属性添加@synthesize指令后,属性视图无法显示

时间:2010-03-02 14:41:44

标签: objective-c iphone properties uiviewcontroller xib

我有一个mainwindow.xib,它有一个tabbar控制器。第一个标签栏有一个视图,从“View1.xib”加载。在我的View1.xib中,我将UI元素拖到它上面。它有.h:

#import <UIKit/UIKit.h>
@class View1Controller;


@interface View1Controller : UIViewController {
    IBOutlet UIView *view;
    IBOutlet UIButton *startButton;
}

@property (retain, nonatomic) UIView *view;
@property (retain, nonatomic) UIButton *startButton;


-(IBAction)startClock:(id)sender;

@end

在.m文件中,我什么都不做。它会表现正常。我可以看到视图及其按钮。但是在我添加之后:

@synthesize view, startButton;

当我加载应用程序时,它显示一个空白视图,没有按钮,但不会产生错误。发生了什么事?

2 个答案:

答案 0 :(得分:2)

基本问题是UIViewController已经拥有view属性。当您在UIViewController子类中重新定义它时,将覆盖该视图属性。坦率地说,我甚至惊讶它甚至编译。

修复:

(1)首先,问问自己是否需要继承的旁边的另一个视图属性。如果只需要控制器的一个视图,则只使用继承的属性。

(2)如果您确实需要引用第二个视图,请将其命名为:

#import <UIKit/UIKit.h>
//@class View1Controller; <-- don't forward declare a class in its own header


@interface View1Controller : UIViewController {
    // IBOutlet UIView *view; <-- this is inherited from UIViewController
    IBOutlet UIView *myView;
    IBOutlet UIButton *startButton;
}
//@property (retain, nonatomic) UIView *view; <-- this is inherited from UIViewController
@property (retain, nonatomic) UIView *myView;
@property (retain, nonatomic) UIButton *startButton;


-(IBAction)startClock:(id)sender;

@end

然后在实施中:

//@synthesize view; <-- this is inherited from UIViewController
@synthesize myView, startButton;

答案 1 :(得分:1)

问题是您将viewstartButton变量声明为IBOutlets。这意味着Interface Builder直接从XIB文件绑定这些变量。

您定义的属性不是IBOutlets。合成它们时,会覆盖getter / setter,Interface Builder无法再绑定它们。

要解决您的问题,请从您的成员变量中删除IBOutlet说明符,并将您的属性声明更改为以下内容:

@property (retain, nonatomic) IBOutlet UIView *view;
@property (retain, nonatomic) IBOutlet UIButton *startButton;