2010-03-02 16 views
1

我有一個具有tabbar控制器的mainwindow.xib。第一個標籤欄有一個視圖,從「View1.xib」加載。在我的View1.xib中,我拖動UI元素。它有.h:iPhone:添加屬性的@synthesize指令後,該屬性無法顯示

#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; 

當我加載應用程序時,它顯示一個空白的視圖,沒有按鈕,但沒有產生錯誤。發生什麼事?

回答

1

問題是,您將您的viewstartButton變量聲明爲IBOutlets。這意味着Interface Builder直接從XIB文件中綁定這些變量。

雖然您定義的屬性不是IBOutlets。當你合成它們時,你覆蓋了getters/setter,Interface Builder不能再綁定它們。

解決您的問題,從你的成員變量中刪除IBOutlet中符和改變你的屬性聲明以下幾點:

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

我同意,在合成將創建一個getter和setter方法爲您的財產,但沒有IBOutlet它不會涉及到XIB,因爲你重寫視圖你得到一個空白。這是我的看法,即使你不觸及UIViewController,IBOutlet也可以工作。 IBOutlet UIButton * startButton; } – Paulo 2013-09-29 15:40:45

2

的基本問題是,UIViewController已經有一個view屬性。當您在UIViewController子類中重新定義它時,將覆蓋該視圖屬性。坦率地說,我很驚訝它甚至編譯。

要解決:

(1)首先,問自己,如果你需要繼承一個旁邊另一種觀點認爲酒店在所有。如果您只需要一個控制器視圖,只需使用繼承的屬性即可。

(2)如果你確實需要一個referece第二視圖,其命名是這樣的:

#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; 
+0

+1,這發生在我身上......花了很多試錯法找到,應該首先查閱Stack Overflow! – 2011-05-23 20:06:04