views:

118

answers:

2

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?

A: 

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;
Martin Cote
+1  A: 

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