ios - 从扩展程序打开应用程序,属性仅在第一次更改

标签 ios objective-c iphone ios8 ios8-today-widget

我有一个今天的小部件扩展程序,当我单击一个按钮时,它会打开该应用程序。我第一次单击按钮并按照代码进行操作时,它使用自定义 URL 方案来传递数据。这在 AppDelegate 中进行了解析,并计算出用什么数据填充 ViewControllerViewController 使用 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/

相关文章:

ios - Nativescript-Vue -- 本地镜像路径不起作用

iphone - 带有自定义标题的 UIBarButtonItem

objective-c - 如何在 iOS 开发中切换新的 View Controller ?

android - 哪些工具和技术用于将视频从Youtube App转换到Smart TV?

ios - swift - 将字符串转换为日期,然后转换为不同格式的字符串

ios - 找不到与 Applications/***.app 匹配的配置文件

ios - iOS 中 UIDatePicker 的默认日期

ios - 如何计算 textView 中的单词和字符总数?

iphone - 使用 NSSortDescriptor 时,您可以使用 NSPredicate 将获取的项目放在所有其他项目之前吗

iphone - iOS 6 : background imageview in iPhone 5