iphone - 将值传递给目标 Controller

标签 iphone objective-c ios

我正在将 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 可以是您需要的任何类型,例如 NSStringNSNumber、...)。

然后,更改您的 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/

相关文章:

iphone - NSMutableURLRequest 使用 POST 返回 null

ios - 适合初学者的 Swift 核心图

IOS 8 NSLocationAlwaysUsageDescription 自定义翻译

ios - 收到 APNS 通知但不显示

ios - 如何使用 iPhone 连接到服务器(无浏览器)

ios - 如何将 UIViewController 作为参数传递给 swift 3?

ios - 为什么 CIImage 的属性方法返回 nil

iphone - 检查 NSURLRequest 目标是否为 "_blank"?

ios - 在ios中设置调用多个标签

ios - 如何更改 UISwitch 关闭状态的默认颜色?