我试图在Xcode4中将App Delegate连接到View Controller,但由于某些原因我无法建立连接,因为App Delegate无法在可视对象编辑器中看到它。
在可视对象编辑器中添加了视图控制器,并将类ID更改为HelloWorldViewController,并在App Delegate.h和.m文件中添加了代码。
我做错了什么?
以下是代码:
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 (nonatomic, 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
答案 0 :(得分:1)
您需要为IB添加IBOutlet
标记才能看到它。将属性声明为,
@property (nonatomic, retain) IBOutlet HelloWorldViewController *viewController;
这甚至适用于您要公开的方法,但您将使用IBAction
。例如,
- (IBAction)doSomething;
这两个标签都只是指标,实际上是声明为
#define IBAction void
#define IBOutlet