不了解如何在 ViewController 之间传递值
Not understanding how to pass values between ViewControllers
我已经知道如何在 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] Unknown class _TtC31ProfileControllerViewController31ProfileControllerViewController in Interface Builder file.
2015-06-27 12:31:57.102 Ch4nge.me[41958:1907763] -[UIViewController passUser:]: unrecognized selector sent to instance 0x7fb505412c00
2015-06-27 12:31:57.107 Ch4nge.me[41958:1907763] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController passUser:]: unrecognized selector sent to instance 0x7fb505412c00'
有什么问题吗???
非常感谢您。
-[UIViewController passUser:]: unrecognized selector sent to instance
表示您的 vc
变量不是 ProfileControllerViewController
类型。
可能您没有在 XIB 或 Storyboard 中设置 class,这就是 instantiateViewControllerWithIdentifier
没有返回预期类型的原因;而是返回类型 UIViewController
.
的默认实例
你可以使用另一个选项来解决这个错误,你可以使用 segue,
参考下面link,
How to Send data between view controllers StoryBoard Xcode
您可以使用:
-(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;
我已经知道如何在 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] Unknown class _TtC31ProfileControllerViewController31ProfileControllerViewController in Interface Builder file. 2015-06-27 12:31:57.102 Ch4nge.me[41958:1907763] -[UIViewController passUser:]: unrecognized selector sent to instance 0x7fb505412c00 2015-06-27 12:31:57.107 Ch4nge.me[41958:1907763] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController passUser:]: unrecognized selector sent to instance 0x7fb505412c00'
有什么问题吗???
非常感谢您。
-[UIViewController passUser:]: unrecognized selector sent to instance
表示您的 vc
变量不是 ProfileControllerViewController
类型。
可能您没有在 XIB 或 Storyboard 中设置 class,这就是 instantiateViewControllerWithIdentifier
没有返回预期类型的原因;而是返回类型 UIViewController
.
你可以使用另一个选项来解决这个错误,你可以使用 segue,
参考下面link,
How to Send data between view controllers StoryBoard Xcode
您可以使用:
-(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;