我从一个将 UIViewController 连接到 nib 的教程示例开始,但现在我决定全部以编程方式完成。因此,我删除了 Nib ,但不知道如何实现我的 Controller 。
我做了这样的事情:
EventsDetailController *myChild = [[EventsDetailController alloc] init];
[self.navigationController pushViewController:myChild animated:YES];
但是,当我单击特定单元格时它崩溃了。
我必须初始化一些东西吗?在我有 Nib 之前,它是 initWithNib
#import <UIKit/UIKit.h>
@interface EventsDetailController : UIViewController {
NSString *message;
}
@property (nonatomic, copy) NSString *message;
@end
#import "EventsDetailController.h"
@implementation EventsDetailController
@synthesize message;
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
}
-(void)viewDidLoad{
UILabel *theMsg = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,30)];
theMsg.text = @"hello";
[theMsg release];
[super viewDidLoad];
}
-(void)viewDidUnload{
self.message = nil;
[super viewDidUnload];
}
-(void)dealloc{
[message release];
[super dealloc];
}
@end
也许,我会从头开始,我认为它仍在寻找我删除的 Nib 。
最佳答案
我假设您已将应用委托(delegate)的 window.rootViewController
设置为 NavigationController
实例,并且该实例是使用 initWithRootViewController
,正如您提到的,当您点击一个单元格时,您已经到了崩溃的地步。
查看添加的代码,我发现唯一奇怪的地方是“veiwDidLoad”方法 - 您分配并释放了“theMsg”,但不使用它。我假设您已为简洁起见删除了一些代码。
当我离开 .nib 文件时,我最终从头开始(在启动我的第一个 iPhone 应用程序后大约 5 分钟)。我会这样做,一次添加一点功能。
关于iphone - 我正在尝试初始化要推送的 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7127990/