iOS:无法通过segue传递NSArray
我有两个视图控制器。我想通过segue将一个iOS:无法通过segue传递NSArray,ios,segue,Ios,Segue,我有两个视图控制器。我想通过segue将一个NSArray传输到另一个 我已经看了所有其他的问题,但我的代码仍然不起作用。 我想转移matchObject Viewcontroller.m #import "mapViewController.h" //call map view -(void) callMap { [self performSegueWithIdentifier: @"callMap" sender: self]; } //pass data - (void)pre
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;
我已将序列图像板中的序列正确连接。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;
}
}
我认为问题在于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];
}
}
希望这对你有用。我想这会有用的。。。
在发送程序中传递数组,同时按如下所示使用标识符执行操作
#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;
}
}
而不是
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是否确定MapViewController类中的matchObject包含数据?否。我想将数据传输到MapViewController。ViewController中存在matchObject。tvc不能有matchObject,因为它是UINavigationController。是否要将其传递给导航控制器的根视图控制器?那么它应该是mapViewController*tvc=[nc.viewControllers objectAtIndex:0];如果确实将matchObject作为mapViewController.h的属性,则使用“.”应该可以正常工作,例如tvc.matchObject=self.matchObject。再一次,如果您将matchobject作为mapviewcontroller的属性,则不需要重写setter方法,如果您所做的只是将传递的值分配给_matchobject(参考Ahmed aster),则我已更新代码以参考UIViewController。仍然收到错误。感谢它工作,但我必须使用“self->matchObject”使其工作。从您的问题Pritesh来看,似乎目的地是导航控制器。那么这个答案是如何起作用的呢?UINavigationController没有属性matchObject。您的问题很好,您只需将tvc从UIViewController更改为mapViewController(请参阅我的答案)。也绝对没有必要覆盖setter。如果确实将matchObject作为mapviewController的属性,它仍然可以调用。我刚才看到了关于您问题的讨论。如果出现错误,说[mapviewcontroller viewcontrollers]未识别选择器,则意味着您的目的地实际上不是导航控制器,因此此答案有效。我已更新代码以引用UIViewController。还是会出错。