简体   繁体   English

将数据从一个视图控制器传递到另一个不转换

[英]Pass data from one view controller to another without transitioning

I have View Controller A (VCA) and View Controller B (VCB). 我有视图控制器A(VCA)和视图控制器B(VCB)。 VCA pulls data from an API call and fills in labels, etc. with the returned data. VCA从API调用中提取数据,并使用返回的数据填充标签等。

When this data is retrieved I want to be able to pass some of it to VCB without transitioning to VCB. 检索此数据时,我希望能够将其中的一些数据传递给VCB,而无需转换到VCB。 When it's time to "go" to VCB I want the data to already be there. 什么时候“去”VCB我希望数据已经存在。

Currently I have the two view controllers linked via presenting and presented modal views. 目前,我通过呈现和呈现的模态视图链接了两个视图控制器。 This works fine, when I present VCB I send along the data from VCA. 这很好,当我提出VCB时,我从VCA发送数据。 However, from a UI/UX perspective, I am switching to a sliding panel view. 但是,从UI / UX的角度来看,我正在切换到滑动面板视图。 So you start from VCA and then can swipe left to slide in VCB from the right. 因此,您从VCA开始,然后可以向左滑动以从右侧滑入VCB。

Because of the sliding you can see both view controllers at the same time and so that's why I want VCB to already be complete visually. 由于滑动,您可以同时看到两个视图控制器,这就是为什么我希望VCB已经在视觉上完整。

My current code in VCA is called on a doubleTap gesture recognizer: 我在VCA中的当前代码是在doubleTap手势识别器上调用的:

- (void)showDetailView
{
    // Initialize VCB
    ViewContollerB *vcb = [[ViewContollerB alloc] init];

    //  ················································
    //  Pass data to ViewContollerB
    //  ················································

    vcb.currently = currently;
    vcb.hourly    = hourly;
    vcb.daily     = daily;
    vcb.unitTemp  = unitTemp;

    vcb.currentLocationCity   = currentLocationCity;
    vcb.currentLocationRegion = currentLocationRegion;
    vcb.currentTemp           = currentTemp;

    // Set transition style to cross fade
    vcb.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

    // Present the daily details
    [self presentViewController:vcb animated:YES completion:nil];
}

Like I said before, this works fine. 就像我之前说过的,这很好用。

On the receiving end (VCB), I have things like this in - (void)viewWillAppear:(BOOL)animated 在接收端(VCB),我有这样的事情- (void)viewWillAppear:(BOOL)animated

if (currentTemp) {
    // Displays data from passed variables
    [self displayHeaderInfo];
}

What I would like to do is essentially this in VCA: 我想要做的主要是在VCA中:

// PRETEND DATA RETURNED FROM API CALL IN VCA

if (currentTemp) {
    // Set array for VCB
    vcb.currentTemp = currentTemp;

    **** CODE TO SEND THIS TO VCB ****
    **** BUT NOT GO TO VCB, STAY  ****
    **** RIGHT HERE IN VCA.       ****

    // Display in VCA
    [self displayCurrentTemp];
}

Is this possible? 这可能吗?

In this case, it's best if you assign the role of fetching the data to a separate DataFetcher object. 在这种情况下,最好将分配数据的角色分配给单独的DataFetcher对象。 Don't make your UIViewControllers responsible for fetching the data. 不要让UIViewControllers负责获取数据。

Implement a DataFetcherDelegate or a completion block that will notify the ViewControllers when the data has been fetched. 实现DataFetcherDelegate或完成块,它将在获取数据时通知ViewControllers。

See this answer for an example implementation of the delegate approach. 有关委托方法的示例实现,请参阅此答案

暂无
暂无

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

相关问题 使用按钮从一个视图控制器过渡到另一个 - transitioning from one view controller to another using a button 如何在IOS中将数据从一个视图传递到另一个视图控制器? - How to pass data from one view to another view controller in IOS? 如何使用委托将数据从一个视图控制器传递到另一个视图控制器? - how to use delegate to pass data to from one View Controller to another? 从一个视图控制器转换到另一个视图控制器时,视图方法的调用顺序是什么? - In what order are view methods called when transitioning from one view controller to another? 如何将数据从一个视图控制器传递到导航控制器中嵌入的另一个视图控制器 - How to pass data from one view controller to another view controller which is embedded in navigation controller 在不使用Segue的情况下将数据从一个视图控制器传递到另一个 - Passing data from one view controller to another without using Segue 将NSManagedObject从一个视图控制器传递到另一个视图控制器 - Pass NSManagedObject from one view controller to another 无法将值从一个View Controller传递到另一个View Controller - Not able to pass value from one View Controller to another View Controller Swift:通过TabBar和导航控制器以编程方式从一个视图过渡到另一个视图 - Swift: Programmatically transitioning from 1 view to another via a TabBar and Navigation Controller 从一个视图控制器获取数据到另一个 - getting data from one view controller to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM