我有2个ViewControllers。我想通过segue将NSArray
转移到另一个。
我已经看过关于SO的所有其他问题,但我的代码仍无效。 我想传输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 :(得分: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;
}
}
答案 1 :(得分:1)
我认为问题出在UINavigationController ......
你可以像这样在mapViewController类中为matchObject创建一个setter。
- (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];
}
}
希望这对你有用。
答案 2 :(得分: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
答案 3 :(得分: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;
}
}