xcode 如何将 app Delegate 的对象访问到 RootViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4549115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access objects of app Delegate into RootViewController
提问by Ashutosh
I have a navigation based app in which i am calling a web service. I have done all the work which is required in the background to absorb the web services. The only thing left is to display it in a Table view. The data i want to display is stored in a Mutable array and i can see the data in console and this is in app delegate. I just want to pass this data somehow to root so that i can display it in table view. Could somebody help me with this.
我有一个基于导航的应用程序,我在其中调用 Web 服务。我已经完成了在后台吸收 Web 服务所需的所有工作。剩下的唯一事情就是在表格视图中显示它。我想显示的数据存储在一个可变数组中,我可以在控制台中看到数据,这是在应用程序委托中。我只是想以某种方式将这些数据传递给 root,以便我可以在表视图中显示它。有人可以帮我解决这个问题。
回答by WrightsCS
You can try this:
你可以试试这个:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
cell.textLabel.text = [appDelegate.yourArray objectAtIndex: indexPath.row];
The smarter thing to do is to create an instance of an NSMutableArray in your root view, then have that feed off of the appDelegate array.
更明智的做法是在根视图中创建 NSMutableArray 的实例,然后从 appDelegate 数组中获取该实例。
回答by Madhu
In appDelegate .h file
在 appDelegate .h 文件中
@property(nonatomic,retain)NSMutableArray *array;
in AppDelegate.m file
在 AppDelegate.m 文件中
@synthesis array;
In rootViewController file
在 rootViewController 文件中
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"array is %@",appDelegate.array);
回答by User558
In Appdelgate.h create your string
在 Appdelgate.h 创建你的字符串
@property (strong, nonatomic) NSString *deviceToken;
+ (AppDelegate *)getAppDelegate;
Appdelegate.m implement
Appdelegate.m 实现
+ (AppDelegate *)getAppDelegate{
static AppDelegate* mAppDelegate = nil;
if(!mAppDelegate)
{
UIApplication *lApplication = [UIApplication sharedApplication];
if([lApplication.delegate isKindOfClass: [AppDelegate class]])
{
mAppDelegate = (AppDelegate *)lApplication.delegate;
}
}
return mAppDelegate;
}
and access this string into where you need, create variable for appdelegate
并将此字符串访问到您需要的位置,为 appdelegate 创建变量
AppDelegate *mLoginAppDelegate;
in viewdidload write these lines
在 viewdidload 写这些行
if(!mLoginAppDelegate)
mLoginAppDelegate = [AppDelegate getAppDelegate];
NSLog(@"Device Token: %@", mLoginAppDelegate.deviceToken);
回答by Wayne Hartman
I would likely change your design approach and create a controller that fetches and persists the data. Your are coupling your AppDelegate
to your controller and making it responsible for the data retrieval. This doesn't necessarily fit the MVC approach that is baked into iOS.
我可能会改变你的设计方法并创建一个控制器来获取和保存数据。您将您AppDelegate
的控制器连接到控制器并使其负责数据检索。这不一定适合 iOS 中的 MVC 方法。
By creating a a data object class that is responsible for data retrieval (eg, calling/caching the web service data), you make your code more reusable and easier to use by the parts of your program.
通过创建负责数据检索(例如,调用/缓存 Web 服务数据)的数据对象类,您可以使您的代码更易于重用,并且更易于被程序的各个部分使用。