1

i am wondering that if it is possible to set the delegate of input stream to another class. So far all examples i have encountered are with self: [inputStream setDelegate:self]. I want to set delegate to another class like a ViewController not self. Thanks in advance.

M. Salih Kocak
  • 189
  • 2
  • 14
  • could you solve the problem? Or is it still open? – geo Sep 09 '13 at 10:48
  • I searched delegation mechanism but later i noticed notification center mechanism is more suitable for me. I am creating my singleton `TCPConnection` object and when i get input from server and i post the notification to view controller class to take care of response and make modifications in interface. Thanks for your advices. :) – M. Salih Kocak Sep 13 '13 at 07:54

2 Answers2

2

if your ViewController is responding to NSStreamDelegate, you can initiate an instance of the controller and set the delegate as usual.

@interface ViewController : NSOperation<NSStreamDelegate>
...

-

ViewController *vc = [[ViewController alloc] init];
[inputStream setDelegate:vc];

for example

update:

use an id or UIViewController<NSStreamDelegate> variable in the TCPConnection class to hold the parent.

For example:

// TCPConnection.h

@interface TCPConnection : NSOperation<NSStreamDelegate>

@property(nonatomic, assign) UIViewController<NSStreamDelegate> parent;

-(id)initWithParent:(UIViewController<NSStreamDelegate> *)p_parent;
...

...

// TCPConnection.m

-(id)initWithParent:(UIViewController<NSStreamDelegate> *)p_parent
{
    self = [super init];
    self.parent = p_parent;
    return self;
}

// UIViewController<NSStreamDelegate>.m

TCPConnection *connection = [[TCPConnection alloc] initWithParent:self];

Or a singlton solution, where you always call only

TCPConnection *connection = [TCPConnection sharedInstance];

and have only one instance of this class. For the most cases the best way ;)

geo
  • 1,781
  • 1
  • 18
  • 30
  • Thanks but i think this is a little bit hardcoded because in my mechanism i want to assign that `ViewController` dynamically. For example i create a class named `TCPConnection` which has attributes like inputStream and outputStream. With this i can create this object in every `ViewController` in my project. The problem is, in TCPConnection class how can i know which `ViewController` created the `TCPConnection` object so i can set as delegate that `ViewController`. Do i make myself clear? :) – M. Salih Kocak Sep 03 '13 at 12:06
  • 1
    you can pass the class that created the instance to your TCPConnection (id parent). Already thought if a singleton solution could be good here? So only your TCPConnection handles everything and is just adjusted by the other classes? ;) – geo Sep 03 '13 at 12:46
  • Thank you for your advice i will consider that. :) – M. Salih Kocak Sep 03 '13 at 14:40
  • 1
    updated with an example for assigning parent. hope you know how singletons work. by specific questions feel free to ask ;) if it helped you, please accept and/or vote up :) – geo Sep 03 '13 at 15:07
0

You can typecast the delegate and set it to some particular delegate and that will get called.