![](/img/trans.png)
[英]Is there a function in Swift that only runs during the first time opening the app?
[英]Opening app from extension, properties only change the first time
我有一个今日的窗口小部件扩展程序,当我单击一个按钮时,它将打开该应用程序。 我第一次单击该按钮并遵循该代码,它使用自定义URL方案来传递数据。 这在AppDelegate中进行了解析,并确定了要填充ViewController
数据。 ViewController
用情节提要ID实例化。 将值应用于ViewController
的属性之一,然后在viewDidLoad
中基于传入的Value填充其余值。 所有这些都是第一次。
但是,如果我按下主页按钮,请打开通知中心,在我的应用程序中点击一个按钮,然后再次执行整个过程。.我像往常一样逐步执行代码,所有值均已设置,但是当ViewController
获得时在显示时,值(例如UILabel
)与第一次相同,但应该已更改。
NSString *url = [NSString stringWithFormat:string ://%@", self.expanededTubeLine.lineName ];
NSExtensionContext *myExtension=[self extensionContext];
[myExtension openURL:[NSURL URLWithString:url] completionHandler:nil];
//
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSString *tubeLineName = [url host];
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.koc.extensiontest"];
if ([defaults objectForKey:@"weekendData"]) {
NSData *tubeData = [[defaults objectForKey:@"weekendData"] copy];
TFLParser *parser = [[TFLParser alloc] initWithData:tubeData];
[parser parseData];
for (int x = 0; x < parser.delayedTubeLines.count; x++) {
TubeLine *tl = [[TubeLine alloc] init];
tl = [parser.delayedTubeLines objectAtIndex:x];
if ([tl.lineName isEqualToString:tubeLineName]) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
TubeLineViewController *tubeLineViewController = [storyboard instantiateViewControllerWithIdentifier:@"TubeLineViewController"];
tubeLineViewController.tubeLine = tl;
[self.window.rootViewController presentViewController:tubeLineViewController animated:YES completion:nil];
return YES;
}
}
}
}
//
- (void)viewDidLoad {
[super viewDidLoad];
self.tubeLineName.text = self.tubeLine.lineName;
self.tubeLineName.font = [UIFont openSansLightFontOfSize:18.0f];
self.tubeLineName.textColor = [UIColor whiteColor];
self.tubeLineName.backgroundColor = self.tubeLine.lineBackgroundUIColor;
self.tubeLineName.layer.cornerRadius = 5;
self.tubeLineName.clipsToBounds = YES;
self.tubeLineMessage.font = [UIFont openSansLightFontOfSize:18.0f];
self.tubeLineMessage.text = self.tubeLine.lineMessage;
self.tubeLineMessage.textColor = [UIColor darkGrayColor];
}
听起来好像已经加载了View,所以请先将所有视图控制器弹出,然后再在handleOpenURL:函数中推送基于窗口小部件的控制器。
[self.viewController.navController popToRootViewControllerAnimated:NO];
if ([self.viewController presentedViewController]) {
[self.viewController dismissViewControllerAnimated:NO completion:nil];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.