python小菜 2012-09-24 08:57 采纳率: 0%
浏览 446
已采纳

什么是放松套装? 你是怎么使用它们的?

iOS 6 and Xcode 4.5 has a new feature referred to as "Unwind Segue":

Unwind segues can allow transitioning to existing instances of scenes in a storyboard

In addition to this brief entry in Xcode 4.5's release notes, UIViewController now seem to have a couple of new methods:

- (BOOL)canPerformUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender
- (UIViewController *)viewControllerForUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender
- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier

How do unwind segues work and what they can be used for?

转载于:https://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them

  • 写回答

6条回答 默认 最新

  • ℙℕℤℝ 2013-04-05 16:49
    关注

    In a Nutshell

    An unwind segue (sometimes called exit segue) can be used to navigate back through push, modal or popover segues (as if you popped the navigation item from the navigation bar, closed the popover or dismissed the modally presented view controller). On top of that you can actually unwind through not only one but a series of push/modal/popover segues, e.g. "go back" multiple steps in your navigation hierarchy with a single unwind action.

    When you perform an unwind segue, you need to specify an action, which is an action method of the view controller you want to unwind to.

    Objective-C:

    - (IBAction)unwindToThisViewController:(UIStoryboardSegue *)unwindSegue
    {
    }
    

    Swift:

    @IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {
    }
    

    The name of this action method is used when you create the unwind segue in the storyboard. Furthermore, this method is called just before the unwind segue is performed. You can get the source view controller from the passed UIStoryboardSegue parameter to interact with the view controller that initiated the segue (e.g. to get the property values of a modal view controller). In this respect, the method has a similar function as the prepareForSegue: method of UIViewController.

    iOS 8 update: Unwind segues also work with iOS 8's adaptive segues, such as Show and Show Detail.

    An Example

    Let us have a storyboard with a navigation controller and three child view controllers:

    enter image description here

    From Green View Controller you can unwind (navigate back) to Red View Controller. From Blue you can unwind to Green or to Red via Green. To enable unwinding you must add the special action methods to Red and Green, e.g. here is the action method in Red:

    Objective-C:

    @implementation RedViewController
    
    - (IBAction)unwindToRed:(UIStoryboardSegue *)unwindSegue
    {
    }
    
    @end
    

    Swift:

    @IBAction func unwindToRed(segue: UIStoryboardSegue) {
    }
    

    After the action method has been added, you can define the unwind segue in the storyboard by control-dragging to the Exit icon. Here we want to unwind to Red from Green when the button is pressed:

    enter image description here

    You must select the action which is defined in the view controller you want to unwind to:

    enter image description here

    You can also unwind to Red from Blue (which is "two steps away" in the navigation stack). The key is selecting the correct unwind action.

    Before the the unwind segue is performed, the action method is called. In the example I defined an unwind segue to Red from both Green and Blue. We can access the source of the unwind in the action method via the UIStoryboardSegue parameter:

    Objective-C:

    - (IBAction)unwindToRed:(UIStoryboardSegue *)unwindSegue
    {
        UIViewController* sourceViewController = unwindSegue.sourceViewController;
    
        if ([sourceViewController isKindOfClass:[BlueViewController class]])
        {
            NSLog(@"Coming from BLUE!");
        }
        else if ([sourceViewController isKindOfClass:[GreenViewController class]])
        {
            NSLog(@"Coming from GREEN!");
        }
    }
    

    Swift:

    @IBAction func unwindToRed(unwindSegue: UIStoryboardSegue) {
        if let blueViewController = unwindSegue.sourceViewController as? BlueViewController {
            println("Coming from BLUE")
        }
        else if let redViewController = unwindSegue.sourceViewController as? RedViewController {
            println("Coming from RED")
        }
    }
    

    Unwinding also works through a combination of push/modal segues. E.g. if I added another Yellow view controller with a modal segue, we could unwind from Yellow all the way back to Red in a single step:

    enter image description here

    Unwinding from Code

    When you define an unwind segue by control-dragging something to the Exit symbol of a view controller, a new segue appears in the Document Outline:

    enter image description here

    Selecting the segue and going to the Attributes Inspector reveals the "Identifier" property. Use this to give a unique identifier to your segue:

    enter image description here

    After this, the unwind segue can be performed from code just like any other segue:

    Objective-C:

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

    Swift:

    performSegueWithIdentifier("UnwindToRedSegueID", sender: self)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划