不了解如何在ViewControllers

时间:2015-06-27 11:34:12

标签: ios objective-c uiviewcontroller

我已经知道如何在UIViewController之间传递值或变量。事实上,我在2 + 2 UIViewController之间有2个转换,但工作正常,但我有一个没有做到的。

我试图复制一切,但这个似乎不起作用。

这就是我处理点击以传递到另一个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-06-27 12:31:51.616 Ch4nge.me [41958:1907763] viewDidAppear   2015-06-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'

什么可能是错的???

非常感谢你。

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

您可以使用另一个选项来解决此错误,您可以使用segue,

请参阅以下链接,

How to Send data between view controllers StoryBoard Xcode

答案 2 :(得分: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;