0

I'm developing an app using UITabBarController. More specifically, using the storyBoard. I want all of my tab to be able to send and receive data from the server.

Problem is i don't know how. Only the first tab that have initNetworkCommunications is able to send and receive from the server. So what should i do in order for my app be to be able to send and receive from the other tabs?

I've found that using NSNotificationCentre to handle the data would work but is there another way?

Here's the code for creating the socket connection

-(void)initNetworkCommunication
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"169.254.1.1", 2000, &readStream, &writeStream);

inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;

[inputStream setDelegate:self];
[outputStream setDelegate:self];

[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[inputStream open];
[outputStream open];

}

lets say i have 2 tabs. The first tab has a connect button which is used to call initNetworkCommunication. From this tab i'm able to send and receive data. But what do i do with the other tab? Is there a way to link this connection over?

i've tried to import each other's controller and use [FirstController sendMessage]; from the secondViewController but doesn't seem to work.

2 Answers2

2

Creating a singleton is fine, what I've done is instead of making a class function (which would force your network to re-init the connection every time you switch tabs) I make the networkconnector a property on a custom implementation of tabBar:

#import <Foundation/Foundation.h>
#import "NetworkController.h"

@interface NetworkStorageTabBarController : UITabBarController
@property (nonatomic, strong) NetworkController *thisNetworkController;
@end

And the implementation file:

#import "NetworkStorageTabBarController.h"

@implementation NetworkStorageTabBarController
@synthesize thisNetworkController;
@end

Then when I load up my tabbed view, I call this in viewWillAppear of the first view that will appear:

//set up networking
NetworkStorageTabBarController *thisTabBar = (NetworkStorageTabBarController *) self.tabBarController;
self.thisNetworkController = thisTabBar.thisNetworkController;
self.thisNetworkController.delegate = self;

So far, this has worked gloriously for me. Took me forever to figure it out, so I hope this helps!

Ian
  • 21
  • 2
0

The simplest way is to create a Singleton, let's call it NetworkCommunications.

To make it Singleton (only one instance will be created):

+(NetworkCommunications *)sharedManager {
    static dispatch_once_t pred;
    static NetworkCommunications *shared = nil;

    dispatch_once(&pred, ^{
        shared = [[NetworkCommunications alloc] init];
    });
    return shared;
}

Then you simply call [NetworkCommunications sharedManager] from your tabs to get access to that single instance.

You put your network code in that instance as well.

Resh32
  • 6,500
  • 3
  • 32
  • 40