English 中文(简体)
iPhone: View that is a Property Fails to Appear after Adding a @synthesize Directive for the Property
原标题:

I have a mainwindow.xib which has a tabbar controller. The first tab bar has a view, that loads from "View1.xib". In my View1.xib, I drag the UI element on it. It has this in .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

In .m file, I do nothing. It will behave normally. I can see the view and its button. But after I add :

@synthesize view, startButton;

When I load the application, it shows a blank view with no button but produces no errors. What is happening?

问题回答

The basic problem is that UIViewController already has a view property. When you redefine it in the your UIViewController subclass you overwrite that view property. Frankly, I m surprised it even compiles.

To fix:

(1) First, ask yourself if you need another view property beside the inherited one at all. If you need just one view for the controller, just used the inherited property.

(2) If you do need a referece to second view, name it like this:

#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

then in the implementation:

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

The problem is that you declared your view and startButton variables as IBOutlets. This means that Interface Builder is binding these variables directly from the XIB file.

The properties you defined aren t IBOutlets though. When you synthesize them, you overwrite the getters/setters and Interface Builder cannot bind to them anymore.

To fix your problem, remove the IBOutlet specifier from your member variables and change your property declarations to the following:

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




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签