[英]How to pass data from AppDelegate to ViewController?
我正在使用Safari瀏覽網頁。 單擊此頁面上的按鈕后,我的Ipad將啟動我的應用程序。 所以我在AppDelegate.m
實現了方法- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
。
- (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
)。
現在我想在ViewController
的文本字段中顯示這些字符串。 如何將此NSArray傳遞給ViewController
?
這只是完成它所需的幾行代碼。
將此行放在appDelegate.h文件中
首先,您需要遵循以下協議
@interface AppDelegate : UIResponder <UIApplicationDelegate>
現在需要聲明一個屬性以供使用,如下所示
@property (strong, nonatomic) id<UIApplicationDelegate>delagete;
現在,在視圖控制器中使用屬性的最后一個也是最后一個階段如下
在viewcontroller的.h文件中
首先提到像appdelegate這樣的#import "AppDelegate.h"
然后一個iVar作為AppDelegate *appDel;
在@interface
現在在.m文件中
把appDel = [[UIApplication sharedApplication]delegate];
在視圖中加載方法並訪問appdelegate的所有屬性,如appDel.paramArray
。
快樂編碼:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.