2015-06-27 36 views
0

我已经知道如何在UIViewController之间传递值或变量。事实上,我在2 + 2 UIViewController之间有2次转换,但是我有一个没有这样做的转换。不理解如何在ViewControllers之间传递值

我试图复制一切,但这个似乎并没有工作。

这是我如何处理点击传递到另一个UIViewController

-(void)launchProfile:(int) option { 
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Profile" bundle:nil]; 
    ProfileControllerViewController *vc = [sb instantiateViewControllerWithIdentifier:@"ProfileControllerViewController"]; 

    [vc passValue:_user]; 
    [vc passOption:option]; 
    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentViewController:vc animated:YES completion:NULL]; 
} 

这是我ProfileControllerViewController.h

@interface ProfileControllerViewController : UIViewController 

-(void)passValue:(NSDictionary*)user; 
-(void)passOption:(int)option; 

@property (nonatomic , strong) NSDictionary* user; 

// ... 

@end 

This is my ProfileControllerViewController.m`(在这里我实现该功能的一部分

@implementation ProfileControllerViewController 

int optionSelected = -1; 
const int optionChanges = 0; 
const int optionComments = 1; 

-(void)passOption:(int) option { 
    optionSelected = option; 
} 

- (void) passValue:(NSDictionary *)user_ { 
    _user = user_; 
} 

// ... 

@end 

何时它执行所passValuepassOption误差为:

2015年6月27日12:31:51.616 Ch4nge.me [41958:1907763] viewDidAppear 2015年6月27日12:31:56.041 Ch4nge.me [41958: 1907763] Interface Builder文件中的未知类_TtC31ProfileControllerViewController31ProfileControllerViewController。 2015-06-27 12:31:57.102 Ch4nge.me [41958:1907763] - [UIViewController passUser:]:无法识别的选择器发送到实例0x7fb505412c00 2015-06-27 12:31:57.107 Ch4nge.me [41958:1907763 ***终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因是: ' - [UIViewController中passUser:]:无法识别的选择发送到实例0x7fb505412c00'

可能是错误???

非常感谢您提前。

+1

从日志中,您似乎忘记在故事板中设置类。请检查它 – Leo

+0

也许考虑在'ProfileControllerViewController'中定义属性,然后在launchProfile:中设置它们。它基本上是你现在正在做的同样的事情,但一点点清洁。 – JaredH

回答

1

-[UIViewController passUser:]: unrecognized selector sent to instance表示您的vc变量不是ProfileControllerViewController类型。

可能您没有在XIB或Storyboard中设置类,这就是为什么instantiateViewControllerWithIdentifier未返回预期类型;而是返回类型为UIViewController的默认实例。

+0

因为我之前有很多错误,所以我将Class设置为'ProfileControllerViewController',但将** Module **设置为ProfileControllerViewController(两者)...这就是问题所在......我非常确定这个课程已经确定,这就是为什么我不重新看一看。非常感谢(接受答案,当它使我能够做到这一点时:D) –

1

您可以使用:

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

{ 

    if([segue.identifier isEqualToString:@"segueIdentifer"]) //use whatever identifer you have set 

    { 

     ProfileControllerViewController *vc = (ProfileControllerViewController *)segue.destinationViewController; 

     vc.user = dict; //NSDictionary value 

     vc.option = 1; //Use whatever integer you want to pass 

    } 

} 

并在您的ProfileControllerViewController.h定义@property (nonatomic, strong) NSDictionary* user;@property (nonatomic, assign) int option;

相关问题