2011-05-21 51 views
0

我有一個名爲RootViewController表視圖類和稱爲WifiClass一類提供無線網絡的功能性。當我加載RootViewController類,我調用了一個名爲setupWifiClass方法,它會做WiFi連接的初始化。調用從擁有對象的方法的所有者

當應用程序正在運行,如果任何連接的設備發送一些數據,以我的裝置中,存在在WiFi類將觸發一個流處理程序委託。那時,我需要從我的RootViewController中調用一個名爲myMethod的方法。請任何人都可以告訴我一個好的方法來正確地做到這一點?

+0

可以肯定的是,wifi class實例充當所有web請求的委託。對?這個實例在哪裏初始化? – 2011-05-21 09:43:36

+0

wifi連接類中的所有東西 – Christina 2011-05-21 10:49:26

+0

剛剛調用初始化方法,當我的mainViewController正在加載... – Christina 2011-05-21 10:50:33

回答

0

爲什麼不welcomemessage方法移到appDelegate。這會更有意義,因爲我認爲「消息」不需要與任何特定的視圖控制器相關聯。

因此,當你wifi delegate被觸發只是引用appDelegate並調用方法:

[[[UIApplication sharedApplication] delegate] welcomeMessage]; 
+0

sorry..i需要從我的ViewController或表視圖控制器觸發的方法班級 – Christina 2011-05-21 10:48:55

+0

但這不是你問的。無論如何,MVC仍然可以工作,因爲appdelegate響應方法可以通知viewcontroller中的方法。看到答案 – wuf810 2011-05-22 11:21:20

0

試試這個

[[UIApplication sharedApplication] sendAction:@selector(yourMethod) to:nil from:self forEvent:someEvent]; 
0

很少有事情可以做。一個是對你要調用其方法的對象有一個弱引用。在這種情況下,視圖控制器就是對象。申報MainViewControllerassign -able屬性在WiFi類(假設這是類)和初始化期間將其設置爲視圖控制器。由於您有對視圖控制器的引用,因此可以在委託方法中調用所需的方法。

另一種方法是使用Blocks。塊的定義可以是 -

typedef void (^UpdateHandler)(void); 

... 

@interface WiFiConnection:NSObject <...> { 
    ... 
    UpdateHandler updateHandler; 
} 
... 
- (void)setUpdateHandler:(UpdateHandler)handler; 
@end 

@implemention WiFiConnection 
... 
- (void)setUpdateHandler:(UpdateHandler)handler { 
    updateHandler = handler; 
} 
... 
- (void)delegateMethodFromWhichYouWantToInvoke { 
    ... 
    if (updateHandler != NULL) { 
     dispatch_async(dispatch_get_main_queue(), updateHandler); 
    } 
} 
... 
@end 

現在,您可以在初始化過程中通過更新塊,

WiFiConnection *connection = [[WiFiConnection alloc] init]; 
... 
__block MainViewController *controller = self; 
[connection setUpdateHandler:^{ 
    [controller welcomeMessage]; 
}]; 

有可能是很多在那裏。讓我知道如果它仍然不清楚。閱讀GCD。在我看來,這是一個非常強大的工具。

+0

thankyou..looking on ...... – Christina 2011-05-21 11:26:35

+0

什麼是rootViewController。它是我的mainViewController – Christina 2011-05-21 12:56:26

+0

抱歉.....我可以知道更新處理程序 – Christina 2011-05-21 13:23:56

1

我假設你指的是流處理器的的委託WifiClass?在這種情況下,將您的RootViewController設置爲WifiClass的代理。在委託回調,在RootViewController的實現,調用myMethod的在RVC:

// RootViewController.m 
- (void)delegateCallback { 
    [self myMethod]; 
} 

迴應評論: 在你WifiClass,你必須創建委託的實例變量。

@protocol WifiStreamDelegate 
- (void)handleNewStream:(id)someStreamObject; 
@end 

@interface WifiClass : NSObject { 
    // *delegate* is an object that conforms to the WifiStreamDelegate protocol 
    id<WifiStreamDelegate> delegate; 
    // …Other instance variables  
} 
// You don't want to own your delegate 
// Use the *assign* flag 
@property (nonatomic, assign) id<WifiStreamDelegate> delegate; 
// …Other properties 

@end 

@implementation WifiClass 
@synthesize delegate; 
// …Other methods 
@end 

然後在你的RootViewController的,你必須實現委託和鉤東西:

#import "WifiClass.h" 
@interface RootViewController : UITableViewController<WifiStreamDelegate> 
{ 
    WifiClass *wifi; 
    // …Other instance variables 
} 
// *wifi* is now an object you own—retain it 
@property (nonatomic, retain) WifiClass *wifi 
// …Other properties 
@end 

@implementation RootViewController 
@synthesize wifi; 

- (id)initWithCoder:(NSCoder *)aDecoder { 
    if (!(self = [super initWithCoder:aDecoder])) 
     return nil; 
    if (!self.wifi) 
     wifi = [[WifiClass alloc] init]; 
    // Set delegate 
    wifi.delegate = self; 
} 

- (void)myMethod { 
    // Do something 
} 

// Delegate method 
- (void)handleNewStream:(id)someStreamObject { 
    // Handle stream 
    [self myMethod]; 
} 
@end 

希望這有助於!

+0

我是一個新的人在目標c..so你能解釋一下嗎.. – Christina 2011-05-21 14:22:47

+0

流處理程序是WifiClass的委託.. – Christina 2011-05-21 14:23:13

+0

我需要在Wifi課程中做些什麼改變? – Christina 2011-05-21 14:23:56

0

只是發通知......

後像這樣......

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyMethodNotification" object:self]; 

收到這樣的...

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MyMethod:) name:@"MyMethodNotification" object:nil]; 

你的方法.. ...

  • (void)MyMethod:(NSNotification *)notification { .... .... .... 我們的代碼 }

刪除

[[NSNotificationCenter defaultCenter] removeObserver:self]; 
+0

老實說,代表是爲這種情況而設計的。通知會導致不必要的複雜性和開銷,而IMO會減少封裝。 – FeifanZ 2011-05-21 16:28:30

相關問題