iOS:以编程方式将 Segue 推送到多个 View Controller

标签 ios objective-c

我有一个原始 View Controller 和四个目标 View Controller 。我希望能够使用导航 Controller 推送 segue 到所有 目标 View Controller 。我试过了……

- (IBAction)notificationsButtonPushed:(id)sender {

    NotificationsViewController *notifications = [[NotificationsViewController alloc]init];

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

}

- (IBAction)messagesButtonPushed:(id)sender {

    MessagesViewController *messages = [[MessagesViewController alloc] init];

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

}

- (IBAction)settingsButtonPushed:(id)sender {

      if (canMessage) {

          SettingsViewController *settings = [[SettingsViewController alloc]init];

          [self.navigationController pushViewController:settings animated:YES];
      }

      else {

          NSLog(@"Can't Message");
      }

}

- (IBAction)previewButtonPushed:(id)sender {

    PreviewViewController *preview = [[PreviewViewController alloc]init];

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

}

这只是给我一个没有 UI 组件的空 View Controller 。

注意:我也厌倦了“initWithNidName:”并传入了每个目标 View Controller 的 Storyboard ID,它给我错误:

'Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/B7E025E5-D7D2-4FFD-B49C-E10DF5E94C44/LifePoints.app> (loaded)' with name 'preview' 

我也尝试过...( Storyboard转场设置为“推送”)

    - (IBAction)notificationsButtonPushed:(id)sender {

    [self performSegueWithIdentifier:@"notifications" sender:self];

}

- (IBAction)messagesButtonPushed:(id)sender {

    if (canMessage) {

        [self performSegueWithIdentifier:@"messages" sender:self];

    }

    else {
        NSLog(@"Can't Message");
    }
}

- (IBAction)settingsButtonPushed:(id)sender {

    [self performSegueWithIdentifier:@"settings" sender:self];

}

- (IBAction)previewButtonPushed:(id)sender {

    [self performSegueWithIdentifier:@"preview" sender:self];

}

尽管这确实将目标 View Controller 推送到具有适当转场类型的屏幕上,但它不会转至正确的目标 View Controller 。它似乎只继续到最后一个附加的 Storyboard继续。

有谁知道如何正确应用它并使其以我正在寻找的形式运行?

编辑

重要的是要注意我正在检查“messagesButtonPushed”方法是否满足条件。我正在检查是否允许用户发送消息,如果允许,则转至 VC。

最佳答案

您不需要任何代码来实现基本的转场。

在您的 Storyboard中,确保您的原始 View Controller 嵌入到导航 Controller 中(选择原始 View 并选择编辑->嵌入->导航 Controller )。

然后您可以简单地控制从四个按钮中的每一个拖动到相应的目标 View ,选择“推”作为 segue 类型。然后,您可以单击两个 View 之间的 segue 图标,为每个 segue 分配一个标识符。

您可以删除 IBAction 方法以及链接到它们的按钮上的任何操作。

我建议你完成 Storyboard Tutorial了解 Storyboard和 segues 的工作原理。

如果您确实想以编程方式执行 segue,那么您可以控制从 View 底部的黄色 View Controller 图标到目标的拖动。然后,您可以根据第二个代码使用 performSegueWithIdentifier 调用 segue - 在您的情况下,您可能有两个不同的 segue 并根据购买状态触发商店 segue 或另一个

关于iOS:以编程方式将 Segue 推送到多个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24581450/

相关文章:

objective-c - 如何在Objective-C OpenGL中正确使用InitWindow?

ios - Joomla 网站上的菜单链接在 iPhone 6 Plus 上不起作用

ios - PerformSegueWithIdentifier 和 rootViewController 的问题

iphone - 正则表达式 JS - iPhone 上的 HTML 解析

ios - UITabBarController 中的事件 View

ios - 知道 UITapGestureRecognizer 从 UIView 发送什么标签

objective-c - 具有有效数字的 NSNumberFormatter 格式 0.0 不正确

iphone - 如何在 Core-plot 创建的堆积条中显示每个图的提示值?

iOS 7 Safari 无法滚动 DIV 溢出 : hidden or auto

ios - UIViewController 中的表格 View 没有获得 hidesBarsOnSwipe 功能