![](/img/trans.png)
[英]Same UIView subclass in several UIViewController subclasses?
[英]Can I use the same method passing in different UIViewController subclasses?
我有一個自定義類,該類將消失並與Web服務對話。 目前,我有一個使用服務類的UIViewController子類,並且可以使數據恢復正常。
我將UIViewController子類的引用作為方法參數發送到服務查詢方法(queryServiceWithParent)。
然后,我將UIViewController子類存儲在一個屬性中,以便稍后在該類中使用它。
NeighbourhoodData.h:
NeighbourhoodDetailTableViewController *viewController;
NeighbourhoodData.m:
- (void)queryServiceWithParent:(UIViewController *)controller {
viewController = (NeighbourhoodDetailTableViewController *)controller;
}
我的問題是我現在想從另一個具有不同UIViewController子類的視圖中使用該類。
我可以通過相同的方法傳遞子類,因為我的參數只是一個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
}
}
從那里,您可以根據它們是哪個子類或您想要的任何對象來做不同的事情。
要考慮的另一件事是,如果兩個視圖控制器之間存在共享的功能,則使它們成為一個公共的超類。
您要么創建用於其他類訪問的類方法,要么必須學習設置委托以使當前類成為其他類的委托。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.