【问题标题】:programmatically open specific page in storyboard以编程方式在情节提要中打开特定页面
【发布时间】:2014-09-14 06:28:59
【问题描述】:

我正在使用 plist 来保存一些数据,如下所示, 保存方法

 NSMutableArray * highScores = [NSMutableArray arrayWithObjects:
                                       [NSNumber numberWithInt:597],
                                       [NSNumber numberWithInt:452],
                                       [NSNumber numberWithInt:365],
                                        nil];

        [defaults setObject:highScores forKey:@"high_scores"];

加载方式

 NSMutableArray * highScores = [defaults objectForKey:@"high_scores"];

    for(NSNumber * score in highScores) {
        NSLog(@"Score: %i", [score intValue]);
        if (score >=100) {
            --> OPEN A SPECIFIC UIVIEWCONTROLLER ON THE STORYBOARD
            NSLog(@">100");

        }
    }

当我使用以下代码加载数据时,如果分数 > 100,我希望它在我的故事板上打开一个特定的 uiviewcontroller。我可以知道我该怎么做吗?

谢谢

【问题讨论】:

  • 在此处查看所选答案stackoverflow.com/questions/8348109/… 这应该回答您的问题,因为它提供并回答了如何使用情节提要以编程方式更改视图控制器。作为旁注,我已投票关闭作为重复问题,因为这已被问过数千次,尽管我不认为这值得一票否决。
  • 如果您确实使用该链接,我会添加一个演员表,例如LoginViewController *controller = (LoginViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"LoginIdentifier"]; 这是另一个stackoverflow.com/questions/10933939/…
  • 使用此链接特别打开视图控制器stackoverflow.com/questions/23102978/…
  • @Anbu.Karthik 我喜欢下面的答案,您使用不必要的内存来分配UIStoryboard 的新实例,而应该已经在self.storyboard 下创建了一个实例,为什么要分配不必要的内存来创建故事板的新实例?
  • @Popeye - 我在 iOS 中的经验不足 1 年,但我热爱我的专业和工作,我理解你的问题,如何优化这个

标签: ios objective-c storyboard


【解决方案1】:

使用此代码

        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        viewcontroller *obj = [story instantiateViewControllerWithIdentifier:@"viewcontrollerID"];


        [self.navigationController pushViewController:obj animated:YES];

为此,您必须提供故事板 ID。为此,请参阅我对此的回答 link for storyboardID

【讨论】:

  • 或者你可以告诉他们如何在这个答案中做到这一点。人们不会在答案中过度使用资源,因此请包括在内,因为您在答案中引用了它,如果没有它,您的答案是不完整的。另外,为什么您要获得故事板的新实例,他们应该已经拥有来自self.storyboard 的实例。您的代码将使用不必要的内存。
  • 是的,谢谢。不错。
【解决方案2】:

试试这样可能对你有帮助。

NSMutableArray * highScores = [defaults objectForKey:@"high_scores"];

    for(NSNumber * score in highScores) {
        NSLog(@"Score: %i", [score intValue]);
        if (score >=100) {
            --> OPEN A SPECIFIC UIVIEWCONTROLLER ON THE STORYBOARD
 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]];
        YourViewcontroller*  detail = [storyboard instantiateViewControllerWithIdentifier:@"YourViewcontroller"];
        [self presentViewController:detail animated:YES completion:nil];

 NSLog(@">100");

        }
    }

【讨论】:

  • 你为什么要获得故事板的新实例,他们应该已经有来自 self.storyboard 的实例。您的代码将使用不必要的内存。
猜你喜欢
  • 2016-12-17
  • 2010-12-28
  • 2013-12-13
  • 1970-01-01
  • 2011-04-08
  • 2012-10-13
  • 1970-01-01
  • 2013-10-08
  • 1970-01-01
相关资源
最近更新 更多