在视图控制器之间传递数据

时间:2015-05-05 09:07:14

标签: ios objective-c segue viewcontroller

我试图将一个对象(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];
}

当我尝试在另一个视图控制器中显示对象时,我得到了null:

#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"]);
}

3 个答案:

答案 0 :(得分:2)

您必须在UIViewController方法“prepareSegue...”中设置restaurent。它是在performeSegueWithIdentifier之后调用的,因此目标控制器是可访问的,您可以测试segue.identifier并将restaurent设置为控制器。

示例:

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

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

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

答案 1 :(得分:2)

您需要使用-prepareForSegue来管理这种情况,并且您需要一个iVar来保留餐馆名称。

因此,在地图的.m文件的顶部,添加一个ivar 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];
}

你要做的是用上面的代码替换你的旧代码,你只需要执行segue来调用以下方法。

- (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. 
}

通过这种方式,您可以将地图点击中的对象传递给下一个控制器。你也确定它永远不会是零,因为用户点了它;如果它是零,嗯,他一开始就不能轻拍它!

请注意,我假设您使用字符串作为您的餐馆名称,但如果您更改顶部的ivar,您可以使用任何您想要的东西,只要您可以通过点击地图来检索它< / strong>即可。如果你不能,我需要更多细节来引导你完成另一个解决方案。

问我是否有任何问题,否则这应该有效!

答案 2 :(得分:0)

在ViewController中实现prepareForSegue:sender:方法

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