简体   繁体   English

如何访问其他类中的对象?

[英]How to access objects in other class?

I want to access a function in ViewController 1 from ViewController 2. This works (confirmed by breakpoints and NSLog output). 我想从ViewController 2访问ViewController 1中的一个函数。这有效(由断点和NSLog输出确认)。 But accessing an object which is defined in ViewController 1 is nil when called from ViewController 2. 但是从ViewController 2调用时,访问在ViewController 1中定义的对象为nil

ViewController 1.h ViewController 1.h

#import "SRWebSocket.h"

@interface ViewController : UIViewController <UITextFieldDelegate, SRWebSocketDelegate> 
@property (nonatomic,strong) SRWebSocket *socket;

-(void)sendMessageToServerWithContent:(NSString *)message;

ViewController 1.m ViewController 1.m

- (IBAction)connect:(id)sender {

    socket.delegate = nil;
    socket = nil;

    socket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:serverIP_GLOBAL]]];

    socket.delegate = self;

}

-(void)sendMessageToServerWithContent:(NSString *)message{

//irrelevant code for question removed
[self.socket send:jsonString];

}

ViewController 2.h ViewController 2.h

#import "ViewController.h"

@property (nonatomic,strong) ViewController *viewVC;
@interface ContactsTableViewController : UITableViewController

ViewController 2.m ViewController 2.m

- (void)viewDidLoad {
    [super viewDidLoad];

    viewVC = [ViewController alloc];

    [viewVC sendMessageToServerWithContent:@"Test"]; 
    //When calling from here, the socket object in ViewController 1 is nil

}

You can't initialize the object like: viewVC = [ViewController alloc]; 您不能像这样初始化对象: viewVC = [ViewController alloc];

You have to pass the object of ViewContoller1 to ViewContoller2 ie 您必须将ViewContoller1的对象传递给ViewContoller2,即

When you are pushing the ViewContoller2 on viewcontoller1 you have write storyboard deletgate code 在viewcontoller1上推动ViewContoller2时,您已编写了情节提要删除代码

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {

        ViewController2 *view2=[segue destinationViewController];
        view2.viewVC=self; // pass viewcontoller1 object to viewcontoller2
    }

And In ViewContoller2 You will access the viewContoller1 object in viewVC variable and you can directly call it like this 在ViewContoller2中,您将在viewVC变量中访问viewContoller1对象,您可以像这样直接调用它

- (void)viewDidLoad {

    [super viewDidLoad];
    [viewVC sendMessageToServerWithContent:@"Test"]; 
    //When calling from here, the socket object in ViewController 1 is nil

}

You call viewVC , which even is not properly created and initialized. 您调用viewVC ,甚至没有正确创建和初始化它。 That's not the previous view controller the screen before. 不是屏幕之前的上一个视图控制器。

1.To call the object's method, it should be properly initialized first. 1.要调用对象的方法,应首先对其进行正确的初始化。

viewVC = [[ViewController alloc] init];

You said, the socket property initialized in viewDidLoad method. 您说过, socket属性是在viewDidLoad方法中初始化的。 To force load the view, use the loadView method, if you haven't an intention to present the view first. 要强制加载视图,请使用loadView方法(如果您无意先显示视图)。

2.Don't initialize another ViewController if you want to use the certain object. 2.如果要使用某个对象,请不要初始化另一个ViewController In your case it should be controller from which the second controller presented. 在您的情况下,它应该是显示第二个控制器的控制器。

Somewhere in ViewController.m file, when the second view controller is about to present: 当第二个视图控制器即将出现时,在ViewController.m文件中的某个位置:

ViewController2 *second=[[ViewController2 alloc] init];
second.vc1 = self; // now the second controller has a pointer to original object
[self presentViewController:second ...];

For segue -navigation 用于segue导航

- (void)prepareForSegue:(UIStoryBoardSegue *)segue
{
  ViewController2 *second=(ViewController2 *)segue.destinationViewController;
  send.vc1 = self;
}

Now in ViewController 2 viewDidLoad method 现在在ViewController 2 viewDidLoad方法

- (void)viewDidLoad 
  {
    [super viewDidLoad];
    [self.vc1 sendMessageToServerWithContent:@"Test"]; 
  }

UPD UPD

Why don't you keep your socket in a separate singleton object? 为什么不将套接字保存在单独的单例对象中? That's much more simpler to use. 使用起来更简单。 It would be like: 就像:

[[MySpecialSocketManager sharedInstance] sendMessageToServerWithContent:@"Hello, Server!"];

See, how to implement singleton here 看,如何在这里实现单例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在同一个快速框架中访问其他类? - how to access other class in same swift framwork? 在viewDidLoad中时,如何访问和修改其他对象的属性? - How do I access and modify properties of other objects while in viewDidLoad? 如何在不公开的情况下访问班级中的其他班级成员(ivars)? - How to access other class members(ivars) in a class without making it public? 如何从不同的swift文件访问一个类中的所有对象 - How to access all objects in one class from a different swift file 如何访问其他控制器类中的UIImageView子类中声明的图像的属性? - How to access the properties of image declared in subclass of UIImageView in other controller class? 其他班级的访问按钮 - Access button from other class 从其他类访问变量? - Access variable from other class? 从其他类访问结构 - Access struct from other class 如何使用Swift保存(持久化)包含其他类的类对象 - How do I save (persist) class objects that include other classes with Swift 如何在不影响后续设置的情况下将自己设置为委托class委托给其他对象实现委托方法? - How to set yourself as a delegate class without affecting subsequent settings to delegate to other objects to implement delegate methods?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM