本文介绍了将委托传递给另一个视图控制器的委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在用Objective-C编写的应用程序中构建一项功能.我无法发布代码,但我会尽力描述自己的世界.我面临的问题与代表有关.

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 具有 ConnectionView 作为其 delegate 之一.它有助于对提供给 AlphaViewController 的数据执行一些操作,并在 AlphaViewController的视图上显示输出.

  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.

当您单击 AlphaViewController 中的按钮时,我将显示一个 UIAlertController .我可以从中单击一个按钮以打开 ReportView . BetaViewController 创建 ReportView .

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.

BetaViewController 具有 ReportGenerator 作为其 delegate . ReportGenerator 有助于生成一些HTML,这些HTML是我在 BetaViewController的视图中呈现的.

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

我的问题是,我想使用 ConnectionView的输出(我相信它是 AlphaViewController ConnectionView 对象的一部分),在 ReportGenerator 中对其进行处理,然后在 BetaViewController的视图中以HTML呈现数据.

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. 您可以在推送之前将 ConnectionView的的输出从 AlphaViewController 传递到 BetaViewController .当您将您的 BetaViewController 分配为 ReportGenerator 的委托时,将数据(即 ConnectionView的输出)传递给 ReportGenerator 并获取ReportGenerator 进行处理,然后在 BetaViewController的视图中以HTML呈现数据.
  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. 使用 AppDelegate ,还有第二种方法比上面更简单.您可以在 AppDelegate 类中声明和定义属性,然后从类中的任何位置访问它.

  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

您可以按照您的要求

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.

这篇关于将委托传递给另一个视图控制器的委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 05:26