我有一个今天的小部件扩展,当我单击一个按钮时,它会打开应用程序。我第一次单击按钮并按照代码执行时,它使用自定义 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];
}