如何将数据从AppDelegate传递到ViewController? [英] How to pass data from AppDelegate to ViewController?
问题描述
我正在使用Safari浏览网页.单击此页面上的按钮后,我的Ipad将启动我的应用程序.因此,我在AppDelegate.m
中实现了方法- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
.
I'm using Safari to browse a webpage. After I click a button on this page, my Ipad will launch my app. So I implement the method - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
in the AppDelegate.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;
}
我在网页上使用的网址是myapp://@first_part@second_part@third_part
之类的东西. self.paramArray
存储url的子字符串(myapp://
first_part
second_part
third_part
).
The url I used on the webpage was some thing like myapp://@first_part@second_part@third_part
. self.paramArray
stores the substrings of url (myapp://
first_part
second_part
third_part
).
现在,我想在我的ViewController
的文本字段中显示这些字符串.如何将这个NSArray传递给ViewController
?
Now I want to show these strings in the textfields in my ViewController
. How can I pass this NSArray to the ViewController
?
推荐答案
这只是完成它所需的几行代码.
This is just a few line of code, required to accomplish it.
将此行放入appDelegate.h文件中
Put this line in appDelegate.h file
首先,您需要遵循以下协议
First of all you need to conforms to a protocol as follow
@interface AppDelegate : UIResponder <UIApplicationDelegate>
现在需要声明一个属性供其使用,如下所示:
Now need to declare one property for it to use as follow
@property (strong, nonatomic) id<UIApplicationDelegate>delagete;
现在,在视图控制器中使用属性的最后一个步骤如下:
Now the last and final stage to use the property in view controller is as follow
在viewcontroller的.h文件中
首先放置对appdelegate的引用,例如#import "AppDelegate.h"
然后在@interface
In .h file of viewcontroller
First put reference to appdelegate like this #import "AppDelegate.h"
Then one iVar as AppDelegate *appDel;
in @interface
.m文件中的文件
在视图中放入appDel = [[UIApplication sharedApplication]delegate];
确实加载了方法,并在其中访问了appdelegate的all属性,例如appDel.paramArray
.
Now in .m file
put appDel = [[UIApplication sharedApplication]delegate];
in view did load method and access the appdelegate's all property there like appDel.paramArray
.
快乐编码:)
这篇关于如何将数据从AppDelegate传递到ViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!