我有一个今天的小部件扩展,当我点击一个按钮时,它打开了应用程序。我第一次单击按钮并按照代码,它使用自定义URL方案来传递数据。这是在AppDelegate中解析的,它确定了用ViewController
填充的数据。 ViewController
使用故事板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];
}
答案 0 :(得分:4)
听起来已经加载了View,所以在handleOpenURL:function中推送基于widget的控制器之前弹出所有视图控制器。
[self.viewController.navController popToRootViewControllerAnimated:NO];
if ([self.viewController presentedViewController]) {
[self.viewController dismissViewControllerAnimated:NO completion:nil];
}