0

Is it possible to create an instance of an object in class A and access that same instance of the object from class B? I am trying to develop an app that creates a TCP Socket using NSInputStream and NSOutputStream and need more than one class to be able to access it.

Thank you, Travis Elliott

edit

Here is the code I am working with. Its a program that deals with socket connections. I basically need to be able to communicate to the same socket from my appDelegate and View controller. Here is the code I have based on your help. I am using the appDelegate as the control(D in your example), perhaps I cannot do this. CommunicationHub is the class I need to control the same instance of from both AppDelegate and ViewController.

AppDelegate.h

#import <UIKit/UIKit.h>
#import "ViewController.h"
#import "CommunicationHub.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>{

ViewController *viewController;
CommunicationHub *cHub;
}

@property (strong, nonatomic) UIWindow *window;
@property (strong, retain) ViewController *viewController;
@property (strong, retain) CommunicationHub *cHub;

-(void)CreateInstances;


@end

AppDelegate.m

#import "AppDelegate.h"
#import "ViewController.h"

@implementation AppDelegate
@synthesize viewController;
@synthesize cHub;

@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self CreateInstances];
    // Override point for customization after application launch.
    return YES;
}

-(void)CreateInstances{
    NSLog(@"Inside CreateInstances");
    CommunicationHub *cHub = [[CommunicationHub alloc] init];
    viewController = [[ViewController alloc] init];
    [viewController initWithcHub:cHub];
    NSLog(@"ID of cHub in AppDelegate is %i", cHub);


}

- (void)applicationWillResignActive:(UIApplication *)application
{
    NSLog(@"Application Will Resign Active");
    [cHub disconnect];
}

@end

ViewController.h

#import <UIKit/UIKit.h> 
#import "CommunicationHub.h"

@interface ViewController : UIViewController 
{
    CommunicationHub *cHub;
}

@property (strong, nonatomic) IBOutlet UITextField *IPAddress;
@property (strong, nonatomic) IBOutlet UITextField *PortNumber;
- (IBAction)goAwayKeyBoard:(id)sender;
- (IBAction)touchBackground:(id)sender;
-(void) initWithcHub:(CommunicationHub *)ptr;
- (IBAction)connectSocket:(id)sender;
- (IBAction)disconnectSocket:(id)sender;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize IPAddress;
@synthesize PortNumber;

-(void) initWithcHub:(CommunicationHub *)ptr
{
    cHub = [[ptr retain]init];
    NSLog(@"id of cHub in ViewController is %i", cHub);
}

- (IBAction)connectSocket:(id)sender 
{
    //Called by button on UI.
    int portNumber = [PortNumber.text intValue];

    [cHub Connect:(int *)portNumber ipAddress:(IPAddress.text)];
}

- (IBAction)disconnectSocket:(id)sender 
{
    //Called by button on UI.
    [cHub disconnect];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setIPAddress:nil];
    [self setPortNumber:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

-(IBAction)goAwayKeyBoard:(id)sender{

    [self resignFirstResponder];
}

- (IBAction)touchBackground:(id)sender {
    [IPAddress resignFirstResponder];
    [PortNumber resignFirstResponder];
}


@end

CommunicationHub.h

#import <UIKit/UIKit.h>

NSInputStream *inputStream;
NSOutputStream *outputStream;

@interface CommunicationHub : NSObject <NSStreamDelegate>

- (void)Connect:(int *)port ipAddress:(NSString *)ipAddress;
- (void) disconnect;

@end

CommunicationHub.m

#import "CommunicationHub.h"

@implementation CommunicationHub

- (void)Connect:(int *)port ipAddress:(NSString *)ipAddress 
{
    NSLog(@"inside connect method");

    if ([inputStream streamStatus] == 0 ||[inputStream streamStatus] ==  5 ||[inputStream streamStatus] == 6 ||[inputStream streamStatus] == 7) 
    {
        NSString *myString = ipAddress;
        CFStringRef *myCFString = (__bridge CFStringRef)myString;
        CFReadStreamRef readStream;
        CFWriteStreamRef writeStream;
        CFStreamCreatePairWithSocketToHost(NULL, myCFString, port, &readStream, &writeStream);
        inputStream = (__bridge NSInputStream *)readStream;
        outputStream = (__bridge NSOutputStream *)writeStream;
        [inputStream setDelegate:self];
        [outputStream setDelegate:self];

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

- (void) disconnect
{
    NSLog(@"inside disconnect method");

    if (inputStream != nil) {
        if ([inputStream streamStatus] == 2) {
            NSLog(@"Disconnecting Streams");
            [inputStream close];
            [outputStream close];
        }else {
            NSLog(@"Stream is not Open");
            int status = [inputStream streamStatus];
            NSLog(@"Stream Status is %i", status);
        }

    }else {
        NSLog(@"Input Stream equals Nil");
    }

}

@end
4

1 回答 1

0

长话短说:假设你有 A、B 和 C 类。

C 需要可从 A 和 B 访问。现在,假设我们有控制器 D,它实例化 A 和 B。让 D 先实例化 C,并将其存储在变量中。

C *C_Class = [[[C alloc] init] autorelease];

接下来,用 C 实例化 A 和 B(我在这里假设“保留属性”)

self.A_Class = [[[A alloc] initWithC:C_Class] autorelease];
self.B_Class = [[[B alloc] initWithC:C_Class] autorelease];

在这两个类中,您都需要实例方法:

- (id)initWithC:(C *)C_Class;

在 .m 文件中:

- (id)initWithC:(C *)C_Class {
    C_C = [C_Class retain]; //Assuming a regular instance variable
}

- (void) dealloc {
    [C_C release];  //All that is retained, must be released.
}

从这一刻起,您可以通过名称“C_C”访问 A 类和 B 类中 C 类的一个实例。

现在,请注意,这只是我脑后写的,容易出错,但应该没问题。

我希望它有帮助:)

编辑:由于 OP 似乎对这些代码片段有问题,我将添加更完整的代码。

让我们从控制类“D”开始。

DH:

#import "A.h"
#import "B.h"

@interface D : NSObject

-(void)CreateInstances;

@property (strong, retain) A *A_Class;
@property (strong, retain) B *B_Class;

@end

米:

#import "D.h"
#import "C.h"

@implementation D

-(void)CreateInstances {
    C *C_Class = [[C alloc] init] autorelease];
    self.A_Class = [[[A alloc] initWithC:C_Class] autorelease];
    self.B_Class = [[[B alloc] initWithC:C_Class] autorelease];
}

@end

啊(Bh 反映了这种行为,也许可以考虑对两者都使用超类)

#import "C.h"

@interface A : NSObject {
    C *C_Class;           //Class Reference to the C-Object
}

-(id) initWithC:(C *)ptr; //Initialization method, which takes a 'C'-Object.

@end;

#import "A.h"

@implementation A

- (id) initWithC:(C *)ptr {
    C_Class = [ptr retain]; //So the object doesn't get released prematurely.
}

- (void) dealloc {
    [C_Class release];      //To avoid memory leaks.
}

@end

现在,请记住,我是在脑海中写下这个并没有通过编译器运行它,但它应该可以工作,保存拼写错误等。

另一个编辑:在 OP 添加他自己的代码后,我将在此处发布相关的“更正”代码。

AppDelegate.m

-(void)CreateInstances{
    NSLog(@"Inside CreateInstances");
    cHub = [[CommunicationHub alloc] init]; //We're using the instance variable. Not the local one.
    viewController = [[ViewController alloc] initWithcHub:cHub];
    //[viewController initWithcHub:cHub];  //You may want to rethink your function names.
    NSLog(@"ID of cHub in AppDelegate is %i", cHub);
}

视图控制器.m

//If a method names STARTS with 'init', it SHOULD initialize the object.
-(id) initWithcHub:(CommunicationHub *)ptr
{
    self = [super init];     //Calls the 'init' from the parent class.
    if(self) {
        cHub = [ptr retain]; //Just retain. After all, the object IS initialized already.
        NSLog(@"id of cHub in ViewController is %i", cHub);
    }
    return self;
}

- (void) dealloc {
    [cHub release]; //Basic rule of memory management: ALL that is retained, must be released. In dealloc, at latest.
}

- (IBAction)connectSocket:(id)sender 
{
    //Called by button on UI.
    //Do not pass a pointer to anything, unless you mean to use a pointer.
    [cHub Connect:[PortNumber.text intValue] ipAddress:(IPAddress.text)];
}

其余的应该是正确的,或者至少不是这个问题的主题。

于 2012-07-26T20:28:20.070 回答