使用函数didSelectRowAtIndexPath显示地图

时间:2012-03-16 16:18:20

标签: iphone objective-c xcode uitableview mapkit

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

我的代码如下:

- (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];
}

问题是,当我点击tableview的任何单元格时,程序退出该行的断点:

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

我不知道为什么会这样做以及如何解决这个错误。也许您知道另一种方法(不使用pushviewController函数)。

等待你的答案; - )

此致

昆汀

2 个答案:

答案 0 :(得分:0)

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

App代表内部:

- (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];
}

答案 1 :(得分:-1)

Xib名称

中缺少.xib
UIViewController *mapviewtest=[[MapStation alloc]initWithNibName:@"MapStation.xib"   bundle:nil];