iphone - 我可以使用相同的方法传入不同的 UIViewController 子类吗?

标签 iphone objective-c ios uiviewcontroller

我有一个自定义类,它消失并与网络服务对话。目前我有一个 UIViewController 子类使用我的服务类并正常获取数据。

我将我的 UIViewController 子类的引用作为方法参数发送到服务查询方法 (queryServiceWithParent)。

然后我将 UIViewController 子类存储在一个属性中,以便稍后在类中使用它。

邻里数据.h:

NeighbourhoodDetailTableViewController *viewController;

NeighbourhoodData.m:

- (void)queryServiceWithParent:(UIViewController *)controller {

    viewController = (NeighbourhoodDetailTableViewController *)controller;

}

我的问题是我现在想从另一个具有不同 UIViewController 子类的 View 中使用此类。

我可以通过相同的方法传递子类,因为我的参数只是一个 UIViewController,但需要将我的类属性设置为正确的 UIViewController 子类类型。

我有办法做到这一点吗?我对整个 iPhone 开发场景都很陌生,所以我唯一想到的方法就是创建一个新类,但我认为必须有更好的方法......

最佳答案

你可以问一个id是什么类。这样的事情可能会起作用:

- (void)doStuffWithMyViewController:(UIViewController *)controller
{
    if ([controller isKindOfClass:[FirstViewControllerSubclass class]])
    {
        //The controller is a FirstViewControllerSubclass or a subclass of FirstViewControllerSubclass
    }

    if ([controller isKindOfClass:[SecondViewControllerSubclass class]])
    {
        //The controller is a SecondViewControllerSubclass or a subclass of SecondViewControllerSubclass
    }
}

从那里你可以根据它们是哪个子类或任何你想要的做不同的事情。

另一件要考虑的事情是,如果两个 View Controller 之间存在共享功能,则为它们创建一个通用的父类(super class)

关于iphone - 我可以使用相同的方法传入不同的 UIViewController 子类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5226219/

相关文章:

iphone - 清理 Localized.strings

c++ - 在 Objective-C 中使用 std::list 吗?

IOS内存和内存管理

ios - 使用魔法记录的核心数据迁移

iOS 单元测试 : How to unit test firstResponderStatus

iphone - 更新 UITableView 数据

ios - 如何在特定时间后在 ios 中隐藏 UICollectionView Cell。例如。半小时,7周等

ios - 在 IOS 上调试 Ionic 应用程序?

ios - 从服务器流式传输 .caf 音频文件

ios - 是否允许在同一个类中重复变量名?