关于在视图之间在ios中共享信息 [英] going about sharing information in ios between views

查看:58
本文介绍了关于在视图之间在ios中共享信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是一个一般的理论问题,我希望能引起一些回应.我一直在学习ios,有一个项目正在修改我的视图.它具有一个运行循环,该循环管理一个连接到服务器的套接字,并在传入消息时进行聊天.textview将使用新的聊天进行更新.现在,此文本视图已附加到故事板一.我现在要问的是,当我添加故事板时,他们是否会转到故事板二(我现在正在阅读此多页面方面的内容),尽管用户当然看不到它,但视图一中的聊天是否会继续更新?直到他们返回视野.第二个问题是我可以同时在第一个视图控制器中更新故事板2和一个故事板.我看到您可以在segue的书中将变量传递给故事板2,但是我的套接字运行循环可以驻留在视图控制器中的一个访问视图控制器中的两个变量.如果不是这样,听起来我需要研究某种队列以将更新从telnet传递到故事板2.

解决方案

您从一个非常复杂的项目开始-印象深刻.这里有一些关于情节提要和剧情的快速笔记.

  • 如果您使用视图控制器设置情节提要,然后为另一个视图控制器设置segue-当您选择创建新的视图控制器时,原始视图仍在内存中,如果您仍然可以响应通知或回调这样编程.

    • 当您弹出"或关闭"您紧追的视图控制器时,它已从内存中消失.

    • 进行搜索时,您会通过称为prepareForSeque的方法自动获得对新视图控制器的引用.您可以将此引用存储在一个属性中,并根据需要使用该属性对其进行更新.

这是一个伪示例:

  • 视图控制器a有一些网络代码,我猜是在缓冲区填充时,它会调用视图控制器a中的方法.我们称它为networkMethod作为参考.在网络方法方面,可以说您最终得到一个字符串,并且想要将该字符串传递给视图控制器b,以便可以在那里进行操作.因此,我们不只是设置一个变量,而是将其传递给一个方法,以便您可以对它进行一些操作.希望这种情况符合您的需求,否则本示例的其余部分将不合时宜.

任务-为视图控制器b设置参考. 在您的视图控制器的导入部分中..h文件

#import "ViewControllerB.h"  //this is the name of your view controller b class

在视图控制器a的界面部分(.h文件)中添加:

@property (nonatomic, strong) ViewControllerB *viewB;

在您的.m文件中,您可能会触发搜索-可能是通过按钮或某些操作.听起来您已经有了:

[self performSegueWithIdentifier:@"viewB" sender:self];

现在创建一个新的方法,如下所示:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    self.viewB = segue.destinationViewController;  //this stores a reference for later

}

现在在您的回调方法中,您可以像这样引用视图控制器b:

-(void)networkCallbackMethod:(NSString*)stringData {

    if (self.viewB) {

     [self.viewB myCustomMethodUsingPassedData:stringData];

    }


}

然后最后在视图控制器b中,您将具有如下所示的匹配方法:

-(void)myCustomeMethodUsingPassedData:(NSString*)stringData {
     //update the view here
}

应该大致为您提供一个框架,您可以在其中设置两个视图控制器,查找并获取引用,然后根据需要更新新的视图控制器.

希望有帮助.祝你好运.

I've just a general theory question i was hoping to generate some responses. I've been learning ios and have a project with one view i'm tinkering with. It has a run loop that manages a socket that connects to a server and gets chat as it comes in. a textview is updated with new chat. Now this textview is attached to story board one. I'm going to ask now if they go to story board two when i add it (i'm reading up on this multi page aspect now), would the chat in view one continue to update though the user of course would not see it till they return o the view. The second question is could i update story board 2 and one at the same time in the first view controller. I saw you can pass variables to story board 2 in my book in the segue, but can my socket run loop which lives in view controller one access view controller two variables. If not it sounds like i need to investigate some kind of queue to communicate updates from telnet to story board 2.

解决方案

You're starting off with a pretty complicated project - very impressed. Here are a couple of quick notes on storyboard and segues.

  • if you setup your storyboard using a view controller then setup a segue to another view controller - when you segue the new view controller is created, the original is still in memory and can still respond to notifications or callbacks if you program it that way.

    • when you "pop" or "dismiss" your view controller you segued to, it is gone from memory.

    • when you segue, you automatically get a reference to the new view controller in a method that is called prepareForSeque. You can store this reference in a property and use that property to update it as needed.

So here is a pseudo example:

  • view controller a has some network code, i'm guessing when the buffer is filled it calls a method in view controller a. Let's call it networkMethod as a reference. in side of network method, let say you end up with a string and you want to pass that string over to your view controller b so you can do something with it there. So instead of just setting a variable, we'll pass it in a method, so you can do something with it. Hopefully that scenario is on target with your needs, otherwise the rest of my example will be off base.

task - setup a reference for view controller b. in the import section of your view controller a .h file

#import "ViewControllerB.h"  //this is the name of your view controller b class

in view controller a in the interface section (.h file) add:

@property (nonatomic, strong) ViewControllerB *viewB;

in your .m file you would trigger you segue - maybe on a button or some action. sounds like you already have this:

[self performSegueWithIdentifier:@"viewB" sender:self];

Now create a new method that looks like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    self.viewB = segue.destinationViewController;  //this stores a reference for later

}

Now in your callback method you can reference view controller b like this:

-(void)networkCallbackMethod:(NSString*)stringData {

    if (self.viewB) {

     [self.viewB myCustomMethodUsingPassedData:stringData];

    }


}

then finally in view controller b you would have the matching method like this:

-(void)myCustomeMethodUsingPassedData:(NSString*)stringData {
     //update the view here
}

that should roughly provide you a framework where you can setup two view controllers, segue and grab a reference and then update the new view controller as needed.

hope that helps. good luck.

这篇关于关于在视图之间在ios中共享信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆