故事板prepareForSegue [英] Storyboard prepareForSegue

查看:111
本文介绍了故事板prepareForSegue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对故事板赛段有疑问。平台是iOS 6.1。目标设备是iPhone。 IDE是Xcode 4.5.2。我有2个TableViewControllers,它们都有一个segue(segOne& segTwo)到同一个ViewController。期望的结果是当用户导航到此ViewController时,他们将返回到原始的TableViewController。为此,我需要一些方法将原始segue(或TableViewController)的id传递给目标ViewController。在过去,我通过设置NavigationItem中的后退按钮来完成此操作,该按钮始终正常工作。现在,我想将后退按钮编码为单独的控件。

I have a question regarding storyboard segues. Platform is iOS 6.1. Target device is iPhone. IDE is Xcode 4.5.2. What I have are 2 TableViewControllers, both of which have a segue (segOne & segTwo) to the same ViewController. The desired outcome is that when a user navigates to this ViewController, they will return to the originating TableViewController. To do this I need some way to pass the id of the originating segue (or TableViewController) to the destination ViewController. In the past I accomplished this by setting up the 'back' button in the NavigationItem which always worked fine. Now, however I want to code the back buttons as separate controls.

我明白为了实现这一点,我需要在源TableViewControllers中设置'prepareForSegue'方法我已经做好了。我遇到问题的是在destinationViewControllers中编写代码以编程方式识别两个segue中的哪一个是源segue,segOne或segTwo?两个segue在Storyboard Seque中被标识为'segOne'和'segTwo'。两者都是风格'推'节段,两者都是目标'当前'的细分。除了segue id之外,每个TableViewController实现文件都是相同的。这是我的代码:

I understand that to accomplish this I need to set up the 'prepareForSegue' method in the source TableViewControllers which I have done. Where I am having a problem is writing the code in the destinationViewControllers to programmatically identify which of the two segues is the source segue, segOne or segTwo? Both segues are identified as 'segOne' and 'segTwo' in Storyboard Seque. Both are style 'push' segues and both are destination 'current' segues. Each of the TableViewController implementation files are identical except for the segue ids. Here's my code:

头文件:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;

实施档案:

-(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
  if([[segue identifier] isEqualToString:@"segOne"])
  {
      NSLog (@"TableViewController1: segue.identifier %@", segue.identifier);
      NSLog (@"TableViewController1: self %@", self);
      NSLog (@"TableViewController1: sender %@", (id)sender);
  }
}

输出正是我所期望的。这里是forTableViewController1:

The output is exactly what I would expect. Here it is forTableViewController1:

TableViewController1: segue.identifier segOne
TableViewController1: self <TableViewController1: 0x928eb70>
TableViewController1: sender <UITableViewCell: 0x927a3e0; frame = (0 22; 768 44); text = '  1. birth month and year...'; autoresize = W; layer = <CALayer: 0x928d380>>

所以,我需要的是ViewController的代码,用于识别两个segue中的哪一个,segOne或segTwo是起源的segue。我明白任何代码都需要放在目标ViewController的viewDidAppear方法中。有了这个,在ViewController中编写IBAction以导航到原始的TableViewController将是一件简单的事情。任何有关如何执行此操作的代码示例将不胜感激。 thx ...

So, what I need is the code for the ViewController that identifies which of the two segues, segOne or segTwo is the originating segue. I do understand that any code needs to go in the viewDidAppear method of the destination ViewController. Once I have this it will be a simple matter to code the IBAction in the ViewController to navigate to the originating TableViewController. Any code examples on how to do this would be appreciated. thx...

推荐答案

使用导航视图控制器推送视图控制器时,它将被推到顶部堆栈与视图控制器。

When you push a view controller using a Navigation view controller, it will be pushed on the top of a stack with view controllers.

所以你需要做的就是回到上一个视图控制器,弹出推送的那个。

So all you have to do get back to the previous view controller is to pop the one that got pushed.

将它放在你的IBAction方法中:

Put this in your IBAction method:

[self.navigationController popViewControllerAnimated:YES];

现在,如果你真的想知道使用了什么segue,你可以只传递segue标识符到目标控制器,并把它放在这样的属性中:

Now, if you really want to know what segue that was used, you can just pass the segue identifier to the destination controller, and put it in a property like this:


-(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"segOne"])
    {
        MyOtherViewController *other = (MyOtherViewController *)segue.destinationViewController;
        other.theSegueIUsed = @"segOne";
    }
}

您还需要添加房产到另一个视图控制器头文件:

You also need to add the property to the other view controllers header file:

@property (strong, nonatomic)NSString *theSegueIUsed;

这篇关于故事板prepareForSegue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆