我有一个自定义类,它消失并与网络服务对话。目前我有一个 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/