2012-03-16 55 views
0

我实际上试图在函数didSelectRowAtIndexPath中显示类型为UIViewController的新控制器MKMapViewDelegate 。 我在控制器中有一个tableview,当我点击这个tableview的单元格时,我想显示类型为UIViewController MKMapViewDelegate的新控制器。使用函数didSelectRowAtIndexPath显示地图

我的代码如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
// Navigation logic may go here. Create and push another view controller. 
UIViewController *mapviewtest=[[MapStation alloc]initWithNibName:@"MapStation" bundle:nil]; 
[self.navigationController pushViewController:mapviewtest animated:YES]; 

//Station *station = nil; 
//station = [self.listStation objectAtIndex:indexPath.row]; 
} 

的问题是,当我在程序退出就行断点的实现代码如下的任意单元格点击:

UIViewController *mapviewtest=[[MapStation alloc]initWithNibName:@"MapStation" bundle:nil]; 

我不知道它为什么这样做以及如何解决这个错误。也许你知道另一种方式来做到这一点(不使用pushviewController函数)。

等待您的回答家伙;-)

真诚

昆汀

+0

和梅普斯塔申是的UIViewController的一个子类? – 2012-03-16 16:40:50

+0

是的MapStation是UIViewController的子视图。我想这可能来自我的navigationController.I没有创建一个(因为它是在控制器被调用时创建的,是不是?) – Quentin91360 2012-03-16 16:51:25

+0

navController不是为你创建的。如果你没有navController,当你尝试推送时,没有任何反应。没有崩溃。如果我不得不猜测,我会说你在执行'initWithNibName:bundle:'时有一个错误,或者你的xib中有一个连接到.h文件中不存在的插座的对象。但我没有心情去猜测。检查控制台输出。 – 2012-03-16 17:26:32

回答

0

这听起来像你需要改变方式你设置了一点应用程序。按照下面的设计,你应该没问题。你的主要问题听起来像你没有创建一个你必须做的导航控制器。

里面的应用代表:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Create the Window 
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 

    // Create RootViewController (i.e. Table View) 
    RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; 

    // Create the Navigation Controller 
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController]; 

    // Make the Navigation Controller the RootViewController 
    self.window.rootViewController = navigationController; 

    // Display the Window 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 

的RootViewController的内部:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Create MapStation 
    MapStation *mapviewtest = [[MapStation alloc] initWithNibName:@"MapStation" bundle:nil]; 

    // Push MapStation on to the self.navigationController (i.e you in the RootViewController class right 
    // which is a navigation controller so this will work. 
    [self.navigationController pushViewController:mapviewtest animated:YES]; 

    // mapviewtest is retained by the navigationController so release it 
    [mapviewtest release]; 
} 
+0

谢谢你的回答!问题是在我的appdelegate中,我的根视图控制器是一个标签栏,因为我想在所有程序执行过程中保留它。所以我很困惑管理标签栏和管理你给我的导航控制器。 – Quentin91360 2012-03-18 17:12:28

-1

你的.xib从XIB名失踪

UIViewController *mapviewtest=[[MapStation alloc]initWithNibName:@"MapStation.xib" bundle:nil]; 
+0

我只是试过了,它不起作用:-( – Quentin91360 2012-03-16 16:25:54

+0

检查你的xib文件...确保其文件的所有者被设置为类MapStation – Shubhank 2012-03-16 16:27:08

+0

我也检查过它。文件的所有者被设置为MapStation类。它仍然 – Quentin91360 2012-03-16 16:32:13