Passing Data from App delegate to View Controller

前端 未结 4 852
慢半拍i
慢半拍i 2020-12-05 09:55

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 De

4条回答
  •  伪装坚强ぢ
    2020-12-05 10:27

    Interface:

    @interface MyAppDelegate : NSObject  {
      NSString *myString;
    }
    @property (nonatomic, retain) NSString *myString;
    ...
    @end
    

    and in the .m file for the App Delegate you would write:

    @implementation MyAppDelegate
    @synthesize myString;
        myString = some string;
    @end
    

    Then, in viewcontroller.m file you can fetch:

    MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
    someString = appDelegate.myString;  //..to read
    appDelegate.myString = some NSString;     //..to write
    

提交回复
热议问题