2013-07-19 60 views
1

我有2个ViewControllers。我想通过segue将NSArray转移到另一个。iOS:无法通过segue传递NSArray

我看过所有其他问题,但仍然我的代码无法正常工作。 我想传输matchObject。

Viewcontroller.m

#import "mapViewController.h" 

//call map view 
-(void) callMap { 
    [self performSegueWithIdentifier: @"callMap" sender: self]; 
} 

//pass data 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"callMap"]) { 
     UINavigationController *nc = segue.destinationViewController; 
     UIViewController *tvc = [nc.viewControllers objectAtIndex:0]; 
     tvc.matchObject = self.matchObject; 
    } 
} 

我得到的最后一条语句错误,说财产matchObject没有找到。

我已经定义在 'mapViewController.h'

@property (nonatomic, strong) NSArray *matchObject; 

属性和中合成它mapViewController.m'

@synthesize matchObject = _matchObject; 

我已经正确连接在故事板的SEGUE。

+0

你确定mapViewController类中的matchObject包含数据吗? –

+0

不,我想将数据传输到mapViewController。 matchObject存在于ViewController中。 –

+2

tvc不能拥有一个matchObject,因为它是一个UINavigationController。你的意思是将它传递给导航控制器的根视图控制器? – rdelmar

回答

1

我认为这个问题是与UINavigationController的...

你可以在这样的MapViewController类matchObject二传手。

- (void)setMatchObject:(NSArray *)matchObject 
{ 
    _matchObject = matchObject 
} 

这个更新prepareForSegue方法...

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"callMap"]) { 
     [segue.destinationViewController performSelector:@selector(setMatchObject:) withObject:self.matchObject]; 

    } 
} 

希望这个作品送给你。

+0

我已更新代码以引用UIViewController。仍然收到错误。 –

+0

我更新了我的答案,看看它,让我知道结果... – DevCali

+0

感谢它的工作,但我不得不使用'自我> matchObject',以使其工作。 –

1

UINavigationController没有名为matchObject的属性。 UIViewController也没有。你必须做的是将destinationViewController转换成你的类的一个实例。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"callMap"]) { 
     MapViewController *tvc = (MapViewController *)segue.destinationViewController; 
     tvc.matchObject = self.matchObject; 
    } 
} 
+0

我已经更新,以引用的UIViewController的代码。仍然收到错误。 –

0

我认为这会工作... 传入而performSegueWithIdentifier发件人的阵列,如下图所示。

#import "mapViewController.h" 

-(void) callMap { 
    [self performSegueWithIdentifier: @"callMap" sender: matchObject]; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"callMap"]) { 
     UINavigationController *tvc = segue.destinationViewController; 
     tvc.matchObject = sender; 
    } 
} 
1

而不是

UIViewController *tvc = [nc.viewControllers objectAtIndex:0]; 
tvc.matchObject = self.matchObject; 

而应该有:

mapViewController *tvc = [nc.viewControllers objectAtIndex:0]; 
tvc.matchObject = self.matchObject; 

你之所以仍然得到一个错误是因为UINavigationController的没有一个叫matchObject属性。

类名应以大写字母开头。你说你的班级叫mapViewController.m 它应该是MapViewController.m