开发者

App Delegate can't see View Controller

Am trying to connect the App Delegate to the View Controller in Xcode4, but I can't make the connection for some reason because App Delegate can't see it in the visual object editor.

Have added the view controller in the visual object editor, and changed the class ID to HelloWorldViewController, and have added the code in the App Delegate.h and .m files.

What am I doing wrong?

Here is the code:

testWindowBasedAppDelegate.h

// -- Add a forward reference to the HelloWorldViewController class --
@class HelloWorldViewController;

#import <UIKit/UIKit.h>

@interface testWindowBasedAppAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    //-- create an instance of the view controller --
    HelloWorldViewController *viewController;
}

@property (nonatomi开发者_开发百科c, retain) IBOutlet UIWindow *window;

// -- expose the view controller as a property --
@property (nonatomic, retain) HelloWorldViewController *viewController; 

@end

testWindowBasedAppDelegate.m

#import "testWindowBasedAppAppDelegate.h"
#import "HelloWorldViewController.h"

@implementation testWindowBasedAppAppDelegate


@synthesize window;
//-- synthesize the property--
@synthesize viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    //-- add the new view to the current window --
    [window addSubview:viewController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)dealloc
{
    [viewController release];
    [window release];
    [super dealloc];
}

@end


You need to add an IBOutlet tag for IB to see it. Declare the property as,

@property (nonatomic, retain) IBOutlet HelloWorldViewController *viewController;

This even applies for methods that you want to expose but there you will use IBAction instead. For example,

- (IBAction)doSomething;

Both of these tags are just indicators and are actually declared as,

#define IBAction void
#define IBOutlet
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜