0

I'm working to build one feature in an app written in Objective-C. I can't post code but I will try to be descriptive with my world. The problem i'm facing is related to delegates.

  1. AlphaViewController has ConnectionView as one of its delegate. It helps to add perform some operation on data provided to AlphaViewController and we show output on AlphaViewController's view.

  2. When you click on a button in AlphaViewController, I show an UIAlertController. From which I can click a button to open ReportView. BetaViewController creates ReportView.

  3. BetaViewController has ReportGenerator as its delegate. ReportGenerator helps to generate some HTML which I render in BetaViewController's view.

My problem is that, I wanna use ConnectionView's output (which I believe is part of ConnectionView object in AlphaViewController), in ReportGenerator to process it and then render data in HTML in BetaViewController's view.

I've been messing around to find a solution but haven't been able to figure out anything. I'm not good with delegates.

Please assist me to achieve my goal here. I'm sorry that I can't post the code.

ap147
  • 162
  • 7
Ryan Cyrus
  • 309
  • 5
  • 14

1 Answers1

0
  1. You can pass your output of ConnectionView's from AlphaViewController to BetaViewController before pushing. When you assign your BetaViewController as delegate of ReportGenerator pass data i.e ConnectionView's output to ReportGenerator and get the ReportGenerator to process it and then render data in HTML in BetaViewController's view.

It may seem to be complicated in words but it can be achieved.

  1. There is also second way much simpler than above using AppDelegate. You can declare and define a property in your AppDelegate class and access it from anywhere in class.

    //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
    

Yo can read the value in BetaViewController or ReportGenerator as per your requirement as

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

For for no. of properties you can create Model Class and you access it as defined above.

luckyShubhra
  • 2,731
  • 1
  • 12
  • 19
  • AppDelagate method is better because you can access this anywhere in the project. – dmaulikr Jul 26 '17 at 06:41
  • @luckyShubhra Thank you so much for this solution. It works flawlessly! However, there is an issue here. When I go back from BetaViewController to AlphaViewController, output of ConnectionView disappears from screen. It seems like object is completely passed to BetaViewController but not retained anyway by AlphaViewController. Do you have any opinion on why this is happening? I'm fixing it on my own but anyway wanna know the reason for it. – Ryan Cyrus Jul 26 '17 at 17:48
  • Sorry my mistake. I copied it from my old project not using ARC. Change your property in AppDelegate to strong and nonatomic. – luckyShubhra Jul 27 '17 at 04:00