开发者

Where is _mainViewController declared and initialized?

i recently updated my IDE to XCode 4.0 and saw a strange change in the Utillity-Application boiler-plate-code:

First, the MainViewController.h-File:

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

@interface UtilityAppDelegate : NSObject <UIApplicationDelegate> {
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MainViewController *mainViewControlle开发者_Go百科r;
@end

Question 1: Where is "mainViewController" declared in the first place? I didn't find it anywhere.

In the *.m-File there is a

@synthesize mainViewController=_mainViewController;

statement. So my second question: Where is "_mainViewController" hidden? Can't find a declaration anywhere. It comes somehow out of the main *.nib file I guess.

But there is another problem: I did add a UINavigationController to one of my recent projects and have no need for mainViewController anymore. But when I delete @property and @synthesize out of MainViewController.m/.h , I can't run the app anymore because of this exception:

setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key mainViewController.'

occurring at this line

    int retVal = UIApplicationMain(argc, argv, nil, nil);

in the main.m.

Thx for your help.


Look at you info.plist it's should be declared there. If you created your app with a template it's configured by the plist. There is some implicit declaration done by this, the mainWindow.xib(in my example) contains more than a window. There are the connections to the appDelegate and the viewController, see second screenshot.

The last line of the screenshot:

Where is _mainViewController declared and initialized?

Where is _mainViewController declared and initialized?


Question 1:

When you use a declaration like this, you don't also need to explicitly define the property.

.h

@property (nonatomic, retain) IBOutlet MainViewController *mainViewController;

.m

@synthesize mainViewController=_mainViewController;

Question 2:

_mainViewController is not hidden. It points to mainViewController which is implicitly defined in the @property statement in .h

@synthesize mainViewController=_mainViewController;

This format is used to distinguish between the ivar and other properties. It refers to mainViewController.

Question 3:

You deleted the declarations @property/@synthesize for mainViewController, but it still exists in the nib file (IB). Delete it from IB and you should be good to go.


You're encountering the new ABI for the first time. It is no longer necessary to actually declare variables for properties. If you use @property and @synthesize, a backing ivar will automatically be generated for you.

You're probably getting the KVC error because the NIB still references the old property. You should see a warning about this during compile. In IB, look at your App Delegate; it probably still has an outlet for mainViewController, and you probably are still generating a MainViewController. You need to delete them from the NIB.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜