2014-10-06 32 views
0

調用類中的方法我有一個方法:無法通過選擇從主類

+ (id) showModalFromController: (UIViewController*) controller 
{ 
    AxEmpAuthorizationController * autorizationController = [[self.class alloc] initWithNibName:NSStringFromClass(self.class) bundle:nil]; 
    [autorizationController performSelectorOnMainThread: @selector(showModalFromController:) withObject: controller waitUntilDone: YES]; 
    return [autorizationController authorelease]; 
} 

,我有一個類別AxEmpAuthorizationController+CustomLoginVC.h在我所覆蓋的方法:

- (void) showModalFromController: (UIViewController*) controller 
{ 
    NavigationTopViewController* navigationController = [[NavigationTopViewController allocWithZone: NULL] initWithRootViewController: self]; 
    [controller presentModalViewController: navigationController animated: ![self.class isMain]]; 
    [navigationController release]; 
} 

的問題在於類別中的方法永遠不會被調用,並且我找不到問題。任何幫助?

+1

切勿使用類別來覆蓋Objective-C中的方法。這是未定義的行爲,可能不會像你希望的那樣工作。 – rmaddy 2014-10-06 17:06:10

回答

2

這裏的一個問題是你不重寫該方法。選擇器將是相同的(我相信) - @選擇​​器(showModalFromController :),但方法不是。對於一個他們有不同的返回類型,另一個是一個類方法(以'+'開頭),一個是實例方法(以' - '開頭)。

你需要確保你的替換方法有簽名:

+ (id) showModalFromController: (UIViewController*) controller 

而不是

- (void) showModalFromController: (UIViewController*) controller 

一旦你整理了這一點,你更近了一步。

正如rmaddy在上面評論的那樣,您可能不想使用類別來覆蓋方法。

如果您嘗試替換行爲,請考慮在您需要的位置使用子類並使用新的子類。

如果您嘗試做一些隱身的事情 - 在整個應用程序中替換此方法,請考慮方法調整(仔細考慮此選項)。

0

您誤解了哪些類別;類別不是子類。你對「主類」的引用是不正確的。類別爲您提供了分離源代碼以提高可讀性的能力,或者擴展那些已經設置好的類。也就是說,如果要將所有類別中的所有來源都放在同一個@implementation區塊(可能與#pragma mark -用於分組一致性分開),則應該使用邏輯正確性進行讀取。你所編寫的代碼將來自:

@implementation SomeClass 
+ (id) showModalFromController: (UIViewController*) controller 
{ 
    AxEmpAuthorizationController * autorizationController = [[self.class alloc] initWithNibName:NSStringFromClass(self.class) bundle:nil]; 
    [autorizationController performSelectorOnMainThread: @selector(showModalFromController:) withObject: controller waitUntilDone: YES]; 
    return [autorizationController authorelease]; 
} 
@end 

@implementation SomeClass (MyCategory) 
- (void) showModalFromController: (UIViewController*) controller 
{ 
    NavigationTopViewController* navigationController = [[NavigationTopViewController allocWithZone: NULL] initWithRootViewController: self]; 
    [controller presentModalViewController: navigationController animated: ![self.class isMain]]; 
    [navigationController release]; 
} 
@end 

到這一點:

@implementation SomeClass 
+ (id) showModalFromController: (UIViewController*) controller 
{ 
    AxEmpAuthorizationController * autorizationController = [[self.class alloc] initWithNibName:NSStringFromClass(self.class) bundle:nil]; 
    [autorizationController performSelectorOnMainThread: @selector(showModalFromController:) withObject: controller waitUntilDone: YES]; 
    return [autorizationController authorelease]; 
} 

#pragma mark - MyCategory 
- (void) showModalFromController: (UIViewController*) controller 
{ 
    NavigationTopViewController* navigationController = [[NavigationTopViewController allocWithZone: NULL] initWithRootViewController: self]; 
    [controller presentModalViewController: navigationController animated: ![self.class isMain]]; 
    [navigationController release]; 
} 
@end 

你定義在同一個類中的兩個同名選擇濫用選擇的名稱。如果你想覆蓋靜態方法,你應該使用一個子類。如果您想在同一個類中使用不同的方法,請使用不同的選擇器名稱。