ios 将数据从应用程序委托传递到视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15049924/
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
Passing Data from App delegate to View Controller
提问by Alex
I need to Pass an string from App delegate to my Initial View Controller , Can somebody listed me the best way to do it , also i tried to Save and Retrieve using NS user Defaults, but i doesn't work out properly .
我需要将一个字符串从 App 委托传递到我的初始视图控制器,有人能列出我最好的方法吗,我也尝试使用 NS 用户默认值保存和检索,但我没有正常工作。
采纳答案by Skanda
Interface:
界面:
@interface MyAppDelegate : NSObject {
NSString *myString;
}
@property (nonatomic, retain) NSString *myString;
...
@end
and in the .m file for the App Delegate you would write:
在 App Delegate 的 .m 文件中,你会写:
@implementation MyAppDelegate
@synthesize myString;
myString = some string;
@end
Then, in viewcontroller.m file you can fetch:
然后,在 viewcontroller.m 文件中,您可以获取:
MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
someString = appDelegate.myString; //..to read
appDelegate.myString = some NSString; //..to write
回答by RyanPliske
Here it is for Swift:
这是 Swift 的:
View Controller
视图控制器
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
Furthermore, if you have an object that you want to pass between view controllers (for example, I had CloudKit data I wanted to share) add this to the App Delegate:
此外,如果您有一个要在视图控制器之间传递的对象(例如,我想要共享 CloudKit 数据),请将其添加到App Delegate:
/* Function for any view controller to grab the instantiated CloudDataObject */
func getCloudData() ->CloudData{
return cloudDataObject
}
Then back in the View Controller
然后回到视图控制器
var model : CloudData = self.appDelegate.getCloudData()
回答by Odrakir
You can access your root view controller like this from the app delegate:
您可以像这样从应用程序委托访问您的根视图控制器:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MyViewController* mainController = (MyViewController*) self.window.rootViewController;
[mainController passData:@"hello"];
return YES;
}
回答by Mu Sa
Using Swift 4.2:
使用 Swift 4.2:
Passing Data from AppDelegate to ViewController:
将数据从 AppDelegate 传递到 ViewController:
let yourViewController = self.window?.rootViewController as? YourViewController
yourViewController?.passData(YOUR_DATA) // pass data
let data = yourViewController?.getData() // access data
Passing Data from ViewController to AppDelegate:
将数据从 ViewController 传递到 AppDelegate:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.passData(YOUR_DATA) // pass data
let data = appDelegate.getData() // access data
Add below code to YourViewController or AppDelegate:
将以下代码添加到 YourViewController 或 AppDelegate:
private var data : String? // String? or any type you want
func getData() -> String? {
return data
}
func passData(_ data : String?) {
self.data = data
}