2015-05-05 27 views
1

我試圖從一個視圖控制器到另一個傳遞一個對象(PFObject)通行證數據

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    RestauCardViewController *restauCard = [[RestauCardViewController alloc]init]; 
    RestaurantAnnotation *restoAnnotation = (RestaurantAnnotation *)view.annotation; 

    restauCard.restaurant = restoAnnotation.restaurant; 
    [self performSegueWithIdentifier:@"segueToCard" sender:nil]; 
} 

當我嘗試顯示在其他視圖控制器我得到空的對象:

#import <UIKit/UIKit.h> 
#import <Parse/Parse.h> 

@interface RestauCardViewController : UIViewController 

@property(nonatomic) PFObject *restaurant; 

@end 

這是我的viewDidLoad功能

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    NSLog(@"The restaurant name is : %@",_restaurant[@"nom"]); 
} 
+0

請參閱http://stackoverflow.com/questions/14805954/mkannotationview-push-to-view-controller-when-detaildesclosure-button-is-clicked。不要使用屬性或ivar來存儲選擇,而應將註釋'view'作爲'sender'傳遞。它使代碼更加獨立,並使用api已經提供的功能。 – Anna

回答

2

您需要使用-prepareForSegue來管理這種情況下實現prepareForSegue:sender:方法,以及你需要伊娃保持餐廳的名字。

所以在地圖的.m文件的頂部,添加伊娃NSString

@implementation yourViewController{ 

    NSString *sRestName; //This is empty until the user selects a restaurant 

} 

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    sRestName = //Set the name of your restaurent here, it's just a string. 
       //You could set any other type of object (a restaurent object or a PFOjbect or anything, 
       //just change the ivar accordingly 
    [self performSegueWithIdentifier:@"segueToCard" sender:nil]; 
} 

你所要做的只是更換上面的舊代碼的代碼,你只需要執行繼續調用以下方法。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    if([segue.identifier isEqualToString:@"fromHomeToList"]){ 
     RestauCardViewController *vc = (RestauCardViewController*)segue.destinationViewController; 
     vc.restaurant = sRestName; //here you're just giving the property of the new controller the content of your ivar. 
} 

這樣,您可以將對象從您的地圖龍頭傳遞到您的下一個控制器。你也確定它永遠不會是零,因爲用戶點擊它;如果它是零,那麼,他不可能首先使用它!

請注意,我假設你正在使用您的餐廳名稱的字符串,但如果你改變頂部的伊娃,你可以只要使用任何你想要的,因爲你可以點擊地圖上檢索。如果你不能,我需要更多的細節來引導你通過另一種解決方案。

問我是否有任何問題,否則這應該工作!

+0

謝謝你很多多數民衆贊成在工作:) –

+0

你可以標記答案是正確的,然後:D很高興我可以幫助! –

2

你要設置的UIViewController甲基內餐飲od「prepareSegue...」。在performeSegueWithIdentifier之後調用它,因此可以訪問目標控制器,並且可以測試segue.identifier並將控制器設置爲控制器。

例子:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    if ([segue.identifier isEqualToString:@"segueRestaurentDetails"]) { 

     RestauCardViewController *destController = (RestaurentDetailsViewController *)segue.destinationViewController; 
     destController.restaurant = (RestaurantAnnotation *)view.annotation; 
} 
+0

謝謝,但我如何從prepareForSegue方法訪問視圖? –

+0

您可以在像「@property(nonatomic,strong)UIView * viewToDisplay;'這樣的私有變量中調用」performeSegueWithIdentifier「之前存儲它。然後你得到它像'(RestaurantAnnotation *)_ viewToDisplay.annotation;' –

+0

不要使用這個屬性,它可以從你不需要的類外部訪問。改用伊娃,看到我的答案更多細節。我們的答案都相當接近 –

0

在視圖控制器

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if([segue.identifier isEqualToString:@"segueToCard"]) { 
     RestauCardViewController *controller = (RestauCardViewController *)segue.destinationViewController; 
     controller.restaurant = (RestaurantAnnotation *)view.annotation; 
    } 
} 
+0

謝謝,但我得到這個錯誤:controller.restaurant =(RestaurantAnnotation *)視圖。註解; 使用未聲明的標識符視圖 –

+0

如何在代碼中聲明'view'? –

+0

這裏: - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {self performSegueWithIdentifier:@「segueToCard」sender:nil]; } –