【问题标题】:pushViewController issuepushViewController 问题
【发布时间】:2011-10-19 16:58:05
【问题描述】:

我有一个 TabBar 应用程序和 2 个视图。 一个视图是带有注释和标注等的 MKMapView。一切正常。单击标注中的“UIButtonTypeDetailDisclosure”按钮会触发我的“showDetails”函数(NSLOG ..)。 现在我想在'showDetails'中推送一个DetailView。我必须使用pushViewController 或presentModalViewController

#pragma mark - MKMap methods
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

    if([annotation isKindOfClass:[CustomPlacemark class]])
    {
        MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation title]];
        newAnnotation.pinColor = MKPinAnnotationColorGreen;
        newAnnotation.animatesDrop = YES; 
        newAnnotation.canShowCallout = YES;
        newAnnotation.enabled = YES;
        //newAnnotation.pinColor=MKPinAnnotationColorPurple;


        NSLog(@"Created annotation at: %f %f", ((CustomPlacemark*)annotation).coordinate.latitude, ((CustomPlacemark*)annotation).coordinate.longitude);

        [newAnnotation addObserver:self
                        forKeyPath:@"selected"
                           options:NSKeyValueObservingOptionNew
                           context:@"GMAP_ANNOTATION_SELECTED"];


        UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"leftIconView.png"]];
        newAnnotation.leftCalloutAccessoryView = leftIconView;
        [leftIconView release];

        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];
        [rightButton addTarget:self
                        action:@selector(showDetails:)
              forControlEvents:UIControlEventTouchUpInside];
        newAnnotation.rightCalloutAccessoryView = rightButton;


        [newAnnotation autorelease];

        return newAnnotation;
    }

    return nil;
}


- (void) showDetails:(id)sender {

    NSLog(@"Annotation Click");

    [self.navigationController pushViewController:self.detailViewController animated:YES];
    // I want to push a new View here...


}

不幸的是,这并没有做任何事情(没有错误/没有行动)。我想这是因为我没有navigationController 可以使用pushViewController。

- (void) showDetails:(id)sender {

    NSLog(@"Annotation Click");

    [self.navigationController pushViewController:self.detailViewController animated:YES];
    // I want to push a new View here...


}

单击通知中的“UIButtonTypeDetailDisclosure”按钮时,我正在尝试在 navigationController 中创建和推送 DetailViewController。但 navigationController 为零 知道要在我的“showDetails”函数中添加什么吗?

非常感谢

【问题讨论】:

    标签: iphone cocoa-touch uinavigationcontroller pushviewcontroller


    【解决方案1】:

    如果你想用导航控制器推送视图控制器,那么你的初始视图必须在导航控制器中开始。

    在您的情况下,您的标签栏控制器可能应该放在导航控制器中(如果还没有的话)。然后你可以按照上面的方式调用你的 showDetails 方法。如果它在导航控制器中,也许你可以在你这样做的地方发布代码,我可以尝试提供更多帮助。

    您可以在 applicationDidFinishLaunchingWithOptions: 方法中执行类似的操作

    FirstViewController *fvc = [[FirstViewController alloc] init];
    SecondViewController *svc = [[SecondViewController alloc] init];
    
    UITabBarController *tabcon = [[UITabBarController alloc] init];
    tabcon.viewControllers = [NSArray arrayWithObjects:fvc,svc,nil];
    
    UINavigationController *navcon = [[UINavigationController alloc] initWithRootViewController:tabcon];
    
    [self.window addSubview:navcon.view];
    

    然后当你想要推送一个细节视图控制器时,你只需调用:

    DetailViewController *dvc = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:dvc];
    

    如果您将视图控制器设置为属性,您可以调用 self.viewControllerName,只要确保您也为它们分配内存。像这样将您的标签栏控制器嵌入到导航控制器中,可以免费为您提供所有的窗口装饰(后退按钮、标题栏等),并且当它们出现在屏幕上时,它将适应您的不同视图。

    【讨论】:

      【解决方案2】:

      在向其推送视图之前,您需要有一个导航控制器:

      DetailViewController *detailController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
      
      // this line calls the viewDidLoad method of detailController
      detailController.view.hidden = NO;
      
      UINavigationController *navigationController = [[UINavigationController alloc] detailController];
      navigationController.navigationBarHidden = YES;
      [navigationController setView:detailController.view];
      
      [self.view addSubview:navigationController.view];
      [detailController release];
      [navigationController release];
      

      请注意,如果您在 showDetails: 中执行此操作,这将一次又一次地初始化 navigationController。所以你需要初始化你的导航控制器,例如。 viewDidLoad 然后你就可以将你的 detailController 推到它上面

      【讨论】:

      • 谢谢,我试过了,但它对我不起作用。我收到 2 条警告消息:警告:未找到“-detailController”方法和警告:“UINavigationController”可能无法响应“-detailController”...
      • 为什么会这样:UINavigationController *navigationController = [[UINavigationController alloc] detailController];?你需要initWithRootViewController什么的。
      • 我已经编辑了代码。它应该是[navigationController setView:detailController.view];。对不起我的错误
      猜你喜欢
      • 1970-01-01
      • 2011-01-17
      • 2011-05-17
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多