PushViewController 问题

发布于 2024-11-28 08:17:13 字数 2625 浏览 0 评论 0原文

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

#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”按钮时,我试图在导航控制器中创建并推送 DetailViewController 。但 navigationController 为零 知道要添加到我的“showDetails”功能中吗?

非常感谢

I've got a TabBar application and 2 views.
One view is a MKMapView with annotations and callouts etc. All works well. Clicking on the 'UIButtonTypeDetailDisclosure' button in the callout my 'showDetails' function fires (NSLOG..).
Now I want to push a DetailView in 'showDetails'. I'd have to either use pushViewController or 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...


}

Unfortunately this doesn't do anything (No error / no action). I reckon it's because I don't have a navigationController to use pushViewController.

- (void) showDetails:(id)sender {

    NSLog(@"Annotation Click");

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


}

i am trying to create and push a DetailViewController in the navigationController when my 'UIButtonTypeDetailDisclosure' button in notification is clicked. but navigationController is nil
Any idea what to add to my 'showDetails' function?

Much appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨小沫ゞ 2024-12-05 08:17:13

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

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推送到它上面

You need to have a navigation controller before pushing views on it:

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

Be careful this will initialize navigationController again and again if you do this in your showDetails:. So you need to initialize your navigation controller in eg. viewDidLoad and then you will be able to push your detailController onto it

堇年纸鸢 2024-12-05 08:17:13

如果您想使用导航控制器推送视图控制器,那么您的初始视图必须首先位于导航控制器内。

在您的情况下,您的标签栏控制器可能应该放入导航控制器(如果还没有)。然后您可以按照上面的方式调用 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,只需确保您是也为它们分配内存。像这样将标签栏控制器嵌入到导航控制器中,您可以免费获得所有的橱窗装饰(后退按钮、标题栏等),并且当它们出现在屏幕上时,它将适应您的不同视图。

If you want to push a view controller with a navigation controller, then your initial view must be within the navigation controller to begin with.

In your case, your tab bar controller should probably be put in the navigation controller if it isn't already. Then you can call your showDetails method the way you have done above. If it is in the nav controller, maybe you can post the code where you do that and I can try and offer more assistance.

Here is how you might do something like this in your applicationDidFinishLaunchingWithOptions: method

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

Then when you want to push a detail view controller, you just call:

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

If you set your view controllers as properties, you can call self.viewControllerName, just make sure you are allocating memory for them as well. Embedding your tab bar controller in a navigation controller like this gives you all the window dressing (back buttons, title bar, etc.) for free and it will adapt to your different views as they come on screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文