我正在将 ID 从 UITABLEVIEWCONTROLLER 传递到另一个 UITABLEVIEWCONTROLLER,但它抛出以下错误。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController setCityId:]: unrecognized selector sent to instance 0x75225e0'
这里是 prepareForSegue 函数:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"cityPushToTab"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
featuredViewController *destViewController = segue.destinationViewController;
destViewController.cityId = [productKeys objectAtIndex:indexPath.row];
}
}
在我调用特色 Controller 的 cityId 之前,该功能运行良好。我试图记录打印正确值的 productKeys 但是当我尝试将值分配给目标 View Controller 对象时它正在终止。请帮忙。
最佳答案
您确定 destViewController
属于 featuredViewController
类吗?我确定不是。崩溃日志表明它是一个 UITabBarController
。
我建议创建一个继承自 UITabBarController
的类。我将其称为 MyTabBarViewController
。将 Storyboard 中的标签栏 Controller 的类设置为这个新类。
在 MyTabBarViewController.h
中,创建一个属性:
@property (nonatomic, strong) id cityId;
(请注意,cityId 可以是您需要的任何类型,例如 NSString
、NSNumber
、...)。
然后,更改您的 prepareForSegue
代码:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"cityPushToTab"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
MyTabBarViewController *destViewController = segue.destinationViewController;
destViewController.cityId = [productKeys objectAtIndex:indexPath.row];
}
}
接下来,在标签栏中的 4 个 View Controller 的 .m 文件中,您可以使用此代码访问 cityId
:
// Cast the viewcontroller's tab bar to your class
MyTabBarViewController *tabBarController = (MyTabBarViewController*)self.tabBarController;
// Access your property
id cityId = tabBarController.cityId;
// You can test to see if it works by casting to an NSString and NSLog it
NSString *cityIdString = (NSString*) tabBarController.cityId;
NSLog (@"%@", cityIdString);
关于iphone - 将值传递给目标 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14042220/