我已經嘗試了來自其他帖子的各種建議,但仍然無法正常工作。如何從自定義對象訪問mainViewController方法?
這是我的工作流程。
AppDelegate.m
#import CustomObject.h // cocoaAsyncSocket wrapper with delegates
- create customObject[[alloc] init];
mainViewController.m
- (IBAction)connectDisconnect
{
// Access our custom object inside the TPAppDelegate class object.
TPAppDelegate *appDelegate = (TPAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.customObject connectToIP:host port:port timeout:10.0];
customObject.m
#import mainViewController.h
// custom object delegate
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
// Access methods and properties inside 'mainViewController' class object.
mainViewController *mainView = (mainViewController *)[UIApplication sharedApplication];
// call method
[mainView textViewLog:@"Hello"];
.
.
}
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIApplication textViewLog:]: unrecognized selector sent to instance 0x188750'
目標是讓我的customObject調用mainViewController中的方法。
我錯過了什麼? 或者我是否完全錯誤?
爲什麼不使用一個通知,告訴什麼每個對象都列出了需要更新? ['NSnotificationCenter'](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html) – rckoenes