问题
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.
AlphaViewController
hasConnectionView
as one of itsdelegate
. It helps to add perform some operation on data provided toAlphaViewController
and we show output onAlphaViewController's
view.When you click on a button in
AlphaViewController
, I show anUIAlertController
. From which I can click a button to openReportView
.BetaViewController
createsReportView
.BetaViewController
hasReportGenerator
as itsdelegate
.ReportGenerator
helps to generate some HTML which I render inBetaViewController'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.
回答1:
- You can pass your output of
ConnectionView's
fromAlphaViewController
toBetaViewController
before pushing. When you assign yourBetaViewController
as delegate ofReportGenerator
pass data i.eConnectionView's
output toReportGenerator
and get theReportGenerator
to process it and then render data in HTML inBetaViewController's
view.
It may seem to be complicated in words but it can be achieved.
There is also second way much simpler than above using
AppDelegate
. You can declare and define a property in yourAppDelegate
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.
来源:https://stackoverflow.com/questions/45317314/passing-a-delegate-to-another-view-controllers-delegate