簡體   English   中英

將數據從一個視圖控制器傳遞到另一個不轉換

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

我有視圖控制器A(VCA)和視圖控制器B(VCB)。 VCA從API調用中提取數據,並使用返回的數據填充標簽等。

檢索此數據時,我希望能夠將其中的一些數據傳遞給VCB,而無需轉換到VCB。 什么時候“去”VCB我希望數據已經存在。

目前,我通過呈現和呈現的模態視圖鏈接了兩個視圖控制器。 這很好,當我提出VCB時,我從VCA發送數據。 但是,從UI / UX的角度來看,我正在切換到滑動面板視圖。 因此,您從VCA開始,然后可以向左滑動以從右側滑入VCB。

由於滑動,您可以同時看到兩個視圖控制器,這就是為什么我希望VCB已經在視覺上完整。

我在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];
}

就像我之前說過的,這很好用。

在接收端(VCB),我有這樣的事情- (void)viewWillAppear:(BOOL)animated

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

我想要做的主要是在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];
}

這可能嗎?

在這種情況下,最好將分配數據的角色分配給單獨的DataFetcher對象。 不要讓UIViewControllers負責獲取數據。

實現DataFetcherDelegate或完成塊,它將在獲取數據時通知ViewControllers。

有關委托方法的示例實現,請參閱此答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM