2017-07-26 55 views
0

我正在使用Objective-C編寫的應用程序中構建一個功能。我不能發佈代碼,但我會盡力描述我的世界。我面臨的問題與代表有關。將委託傳遞給另一個視圖控制器的代表

  1. AlphaViewController具有ConnectionView作爲delegate之一。它有助於對提供給AlphaViewController的數據添加執行一些操作,並且我們在AlphaViewController's視圖上顯示輸出。

  2. 當您點擊AlphaViewController中的按鈕時,我將顯示一個UIAlertController。從中我可以點擊一個按鈕打開ReportViewBetaViewController創建ReportView

  3. BetaViewController具有ReportGenerator作爲其delegateReportGenerator有助於生成一些我在BetaViewController's視圖中呈現的HTML。

我的問題是,我想使用ConnectionView's輸出(我相信是在AlphaViewControllerConnectionView對象的一部分),在ReportGenerator處理它,然後在BetaViewController's視圖呈現HTML數據。

我一直在尋找解決方案,但一直無法找出任何東西。我對代表不滿意。

請幫助我實現我的目標。對不起,我無法發佈代碼。

+0

你能指定什麼樣的輸出嗎?字符串,圖像等。 – luckyShubhra

+0

@luckyShubhra輸出屬於ConnectionView類型。 –

回答

0
  1. 可以前推從AlphaViewController通過你的ConnectionView's輸出BetaViewController。當您將BetaViewController作爲ReportGenerator的代表ReportGenerator分配給傳遞數據,即ConnectionView's輸出到ReportGenerator並獲取ReportGenerator來處理它,然後在BetaViewController's視圖中呈現HTML中的數據。

它可能看起來很複雜,但它可以實現。

  1. 還有第二種方法比上述使用AppDelegate簡單得多。你可以在你的AppDelegate類中聲明和定義一個屬性,並可以在課堂的任何地方訪問它。

    //Interface: 
    
    @interface MyAppDelegate : NSObject { 
        NSString *yourString; // Declare your object 
    } 
    
    @property (nonatomic, strong) NSString *yourString; 
    ... 
    @end 
    
    //and in the .m file for the App Delegate  
    @implementation MyAppDelegate 
    
    @synthesize yourString; 
    yourString = some string; 
    
    @end 
    
    //You can access the property to save and retrieve your file. You can save 
    MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate]; 
    appDelegate.yourString = some NSString;  //..to write 
    

呦可以作爲

MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate]; 
someString = appDelegate.myString; //..to read 

對於沒有閱讀BetaViewController或ReportGenerator值按照您的要求。您可以創建模型類的屬性,並按照上面的定義訪問它。

+0

AppDelagate方法更好,因爲您可以在項目的任何位置訪問此方法。 –

+0

@luckyShubhra非常感謝你的解決方案。它完美無瑕!但是,這裏有一個問題。當我從BetaViewController返回到AlphaViewController時,ConnectionView的輸出從屏幕消失。似乎對象完全傳遞給BetaViewController,但不會被AlphaViewController保留。你對此有何看法?我正在自行修復它,但無論如何都想知道它的原因。 –

+0

對不起,我的錯誤。我從不使用ARC的舊項目複製它。將AppDelegate中的屬性更改爲強和非原子。 – luckyShubhra

相關問題