我正在创建一个没有情节提要的应用,从用户单击表格视图单元格到该单元格的详细信息视图,我遇到了麻烦。
在创建和处理表格视图的类中,我具有以下代码:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
PersonDetailViewController *detailViewController;
Person *person = [self.playerArray objectAtIndex:indexPath.row];
detailViewController.person = person;
[[self navigationController] pushViewController:detailViewController animated:YES];
}
因此,我尝试将详细信息视图控制器推到屏幕上,然后在该详细信息屏幕上显示用户信息。我试图找到一种解决方案,但是,我遇到的大多数答案都使用情节提要,而我没有使用情节提要。
在我的PersonDetailViewController.m类中,我有以下代码:
#import "PersonDetailViewController.h"
@interface PersonDetailViewController ()
@end
@implementation PersonDetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.personInfo = [[UITextField alloc] initWithFrame:CGRectMake(45, 30, 200, 40)];
_personInfo.backgroundColor = [UIColor whiteColor];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
我不确定这是否是造成此问题的原因,但是在我的代表中,我有以下几点:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.tableViewController = [[TableViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.tableViewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
我当时在想,这可能与创建导航控制器有关,但是我一直在玩它,但我认为它并没有。
最佳答案
您错过了tableView:didSelectRowAtIndexPath:方法中视图控制器的初始化
PersonDetailViewController *detailViewController = [[PersonDetailViewController alloc] init]; // or initWith...
关于ios - 应用程序尝试将nil View Controller 插入目标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24940335/