将数据从App委托传递给视图控制器

时间:2022-02-28 12:51:28

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用户默认值来保存和检索,但是我做得不好。

3 个解决方案

#1


28  

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委托的。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

#2


17  

Here it is for 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委托中:

    /* 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()

#3


14  

You can access your root view controller like this from the app delegate:

你可以像这样从app委托访问根视图控制器:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    MyViewController* mainController = (MyViewController*)  self.window.rootViewController;
    [mainController passData:@"hello"];

    return YES;
}

#1


28  

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委托的。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

#2


17  

Here it is for 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委托中:

    /* 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()

#3


14  

You can access your root view controller like this from the app delegate:

你可以像这样从app委托访问根视图控制器:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    MyViewController* mainController = (MyViewController*)  self.window.rootViewController;
    [mainController passData:@"hello"];

    return YES;
}