我正在创建一个没有故事板的应用程序,我无法从用户点击表格视图单元格移动到该单元格的详细视图。
在我创建和处理表视图的类中,我有以下代码:
-(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;
}
我在想,也许它与创建导航控制器有关,但我正在玩它并且我认为它没有。
答案 0 :(得分:3)
你错过了tableView中视图控制器的初始化:didSelectRowAtIndexPath:method
PersonDetailViewController *detailViewController = [[PersonDetailViewController alloc] init]; // or initWith...