如何将数据从AppDelegate传递到ViewController?

Man*_*ted 2 uiviewcontroller ios appdelegate

我正在使用Safari来浏览网页.点击此页面上的按钮后,我的Ipad将启动我的应用程序.所以,我实现该方法- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)urlAppDelegate.m.

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    if (!url) {  return NO; }   
    NSString *URLString = [url absoluteString];
    self.paramArray = [URLString componentsSeparatedByString:@"@"];
    NSLog(@"%d",[self.paramArray count]);
    for (int i = 1; i < [self.paramArray count]; i++) {
        NSLog([self.paramArray objectAtIndex:i]);
    }
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

我在网页上使用的网址有些像myapp://@first_part@second_part@third_part.self.paramArray存储url(myapp:// first_part second_part third_part)的子字符串.

现在我想在我的文本域中显示这些字符串ViewController.我怎样才能将这个NSArray传递给ViewController

The*_*Dev 7

这只是完成它所需的几行代码.

将此行放在appDelegate.h文件中

首先,您需要遵循以下协议

@interface AppDelegate : UIResponder <UIApplicationDelegate>
Run Code Online (Sandbox Code Playgroud)

现在需要为它声明一个属性,如下所示

@property (strong, nonatomic) id<UIApplicationDelegate>delagete;
Run Code Online (Sandbox Code Playgroud)

现在,在视图控制器中使用该属性的最后和最后阶段如下

在viewcontroller的.h文件中
首先将appdelegate的引用添加到这个#import "AppDelegate.h"
然后一个iVar AppDelegate *appDel;@interface

现在在.m文件中
放入appDel = [[UIApplication sharedApplication]delegate];视图做了加载方法并访问appdelegate的所有属性就好了appDel.paramArray.

快乐编码:)