1

我有一個TabBar應用程序和2個視圖。 一個視圖是帶有註釋和標註等的MKMapView。所有效果都很好。點擊標註中的'UIButtonTypeDetailDisclosure'按鈕我的'showDetails'函數會觸發(NSLOG ..)。 現在我想在'showDetails'中推送一個DetailView。我必須要麼使用pushViewControllerpresentModalViewControllerpushViewController問題

#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... 


} 

我想創建並在navigationController推DetailViewController我的「UIButtonTypeDetailDisclosure」在通知按鈕被點擊時。但navigationController是零 任何想法添加到我的'showDetails'功能?

非常感謝

回答

0

你需要有它推前視圖導航控制器:

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到它

+0

謝謝,我試過但它不適合我。我得到2警告消息:警告:沒有'-detailController'方法找到 和警告:'UINavigationController'可能不會響應'-detailController'... – Jan

+0

爲什麼這樣:'UINavigationController * navigationController = [[UINavigationController alloc] detailController] ;'?你需要**'initWithRootViewController' **或者其他東西。 – WrightsCS

+0

我編輯了代碼。它應該是'[navigationController setView:detailController.view];'。抱歉,是我的錯 –

0

如果你想推一個視圖控制器導航控制器,那麼你的初始視圖必須是導航控制器開始與內。

在你的情況下,如果你的標籤欄控制器不是已經存在的話,你應該把它放在導航控制器中。然後你可以按照上面的方式調用你的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,只要確保你爲它們分配內存。將標籤欄控制器嵌入到導航控制器中,這樣可以免費提供所有的窗口修飾(後退按鈕,標題欄等),並且可以在屏幕上適應不同的視圖。

+0

我該怎麼做? Ta – Jan

+0

@Jan我加了一些代碼作爲例子。希望這可以幫助。 – Jamie