3

I've tried the various suggestions from other posts and still can't seem to get this working properly.

Here is my workflow.

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'

The goal is to get my customObject call a method in the mainViewController.

What am I missing? Or am I going about it completely wrong?

Sebastian Dwornik
  • 2,526
  • 2
  • 34
  • 57
  • Why not use a Notification to tell what every object is listing that it needs to update? [`NSnotificationCenter`](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html) – rckoenes Nov 10 '11 at 15:47

3 Answers3

3

when you ask [UIApplication sharedApplication] in this line

mainViewController *mainView = (mainViewController *)[UIApplication sharedApplication];

you got UIApplication instance. Ofcource it's not a mainViewController.

I see a few solutions here:

  1. pass pointer to your mainViewController to method in customObject
  2. implement delegate pattern: define CustomObjectDelegate protocol, add delegate property to CustomObject, set mainViewController as delegate, work with mainViewController from CustomObject as with it delegate.
  3. If delegate already used in CustomObject for something else, than you could create delegate's analog (for example as UITableView has delegate and dataSource)
  4. create some property (i.e. mainViewController) in your application delegate class and set mainViewController to it. Than from anywhere you could ask application delegate as [[UIApplication sharedApplication] delegate] and get your mainViewController like [[[UIApplication sharedApplication] delegate] mainViewController].
yas375
  • 3,940
  • 1
  • 24
  • 33
  • I just implemented #4. :) Good list of options. Thank you. Is there a preference in terms of which one of these is also considered as 'best practice'? – Sebastian Dwornik Nov 10 '11 at 16:55
0

You can do this by creating a protocol and delegate method

First create object of mainView then create customObject's instance
create a protocol and implement it in your customObject class and after creating the customObject set the delegate as mainView.
Implement protocol methods in mainView then you can call a protocol method from customObject that will invoke the delegate method in mainView.
from the delegate method in mainView you can call any method that belongs to mainView

In your code,
This line have issues:

   mainViewController *mainView = (mainViewController *)[UIApplication sharedApplication];

[UIApplication sharedApplication]; does not gives you any controller.

sharedApplication Returns the singleton application instance.

  • (UIApplication *)sharedApplication

Return Value The application instance is created in the UIApplicationMain function.

Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41
0

Another approach I've used successfully is the UIKitCategories approach from:

Get to UIViewController from UIView?

Taimoor Suleman
  • 1,588
  • 14
  • 29
ader
  • 5,403
  • 1
  • 21
  • 26