我已经知道如何在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
当它执行passValue
或passOption
时,错误是:
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'
什么可能是错的???
非常感谢你。
答案 0 :(得分:1)
-[UIViewController passUser:]: unrecognized selector sent to instance
表示您的vc
变量不属于ProfileControllerViewController
类型。
可能你没有在XIB或Storyboard中设置类,这就是instantiateViewControllerWithIdentifier
没有返回预期类型的原因;而是返回类型为UIViewController
的默认实例。
答案 1 :(得分:1)
答案 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;