我整天都在努力解决这个问题,这似乎很简单,但我无法弄清楚。
我有一个使用 XCode 中的“基于视图的应用程序”模板创建的 iOS 应用程序。这基本上是我拥有的代码:
AppDelegate.h:
#import <UIKit/UIKit.h>
@interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MainViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MainViewController *viewController;
@end
AppDelegate.m:
#import "AppDelegate.h"
#import "MainViewController.h"
@implementation AppDelegate
@synthesize window, viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
主视图控制器.h:
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController {
}
-(IBAction) button:(id)sender;
@end
主视图控制器.m:
#import "MainViewController.h"
#import "ModalViewController.h"
@implementation MainViewController
...
-(IBAction) button:(id)sender {
ModalViewController *mvc = [[[ModalViewController alloc] initWithNibName:NSStringFromClass([ModalViewController class]) bundle:nil] autorelease];
[self presentModalViewController:mvc animated:YES];
}
@end
ModalViewController 没有什么有趣的地方。
因此,按下按钮时应显示模态视图。当我按下按钮时,它会挂起一秒钟然后崩溃回到主屏幕,没有错误消息。
我被难住了,请告诉我我做错了什么!