我有一个今天的小部件扩展程序,当我单击一个按钮时,它会打开该应用程序。我第一次单击按钮并按照代码进行操作时,它使用自定义 URL 方案来传递数据。这在 AppDelegate 中进行了解析,并计算出用什么数据填充 ViewController
。 ViewController
使用 Storyboard ID 进行实例化。将值应用于 ViewController
的其中一个属性,然后在 viewDidLoad
中根据传入的值填充其余值。这一切都是第一次。
但是,如果我按下主页按钮,打开通知中心,点击我的应用程序中的一个按钮并第二次执行整个过程。我像往常一样单步执行代码,所有值都已设置,但是当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 已经加载,所以在将基于小部件的 Controller 推送到 handleOpenURL: 函数之前弹出所有 View Controller 。
[self.viewController.navController popToRootViewControllerAnimated:NO];
if ([self.viewController presentedViewController]) {
[self.viewController dismissViewControllerAnimated:NO completion:nil];
}
关于ios - 从扩展程序打开应用程序,属性仅在第一次更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26126944/