[英]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!"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.