简体   繁体   English

将委托传递给另一个视图控制器的委托

[英]Passing a delegate to another view controller's delegate

I'm working to build one feature in an app written in Objective-C. 我正在用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 . AlphaViewControllerConnectionView作为其delegate It helps to add perform some operation on data provided to AlphaViewController and we show output on AlphaViewController's view. 它有助于添加对提供给AlphaViewController数据执行一些操作,我们在AlphaViewController's视图上显示输出。

  2. When you click on a button in AlphaViewController , I show an UIAlertController . 当您单击AlphaViewController的按钮时,我将显示一个UIAlertController From which I can click a button to open ReportView . 从中可以单击一个按钮以打开ReportView BetaViewController creates ReportView . BetaViewController创建ReportView

  3. BetaViewController has ReportGenerator as its delegate . BetaViewController具有ReportGenerator作为其delegate ReportGenerator helps to generate some HTML which I render in BetaViewController's view. ReportGenerator有助于生成一些HTML,这些HTML在BetaViewController's视图中呈现。

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. 我的问题是,我想用ConnectionView's输出(我相信是的一部分ConnectionView在对象AlphaViewController ),ReportGenerator处理它,然后呈现HTML数据BetaViewController's看法。

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 from AlphaViewController to BetaViewController before pushing. 您可以在推送之前将ConnectionView's输出从AlphaViewControllerBetaViewController When you assign your BetaViewController as delegate of ReportGenerator pass data ie ConnectionView's output to ReportGenerator and get the ReportGenerator to process it and then render data in HTML in BetaViewController's view. 当分配BetaViewController为代表ReportGenerator通数据,即ConnectionView's输出ReportGenerator并获得ReportGenerator处理它,然后呈现HTML数据BetaViewController's看法。

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 . 还有第二种方法比使用AppDelegate方法简单得多。 You can declare and define a property in your AppDelegate class and access it from anywhere in class. 您可以在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 

Yo can read the value in BetaViewController or ReportGenerator as per your requirement as 您可以按照您的要求在BetaViewController或ReportGenerator中读取值

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. 可以创建模型类的属性,并按上面的定义访问它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM