在展开后继续推进

我正在使用模式显示相机视图的相机应用程序。 我完成了种植。 我执行一个unwind继续到MainPageViewController 。 (请看截图)

故事板

MainPageViewController我的展开函数如下;

 @IBAction func unwindToMainMenu(segue: UIStoryboardSegue) { self.performSegueWithIdentifier("Categories", sender: self) } 

其中“类别”是从MainPageViewControllerCategoriesTableViewController的push segue标识符。

程序进入unwindToMainMenu函数,但是它不执行push segue。 任何想法如何解决这个问题?

注:我发现了同样的问题,但答案build议更改故事板结构。

晚了一点晚了,但我find了一种不使用状态标志的方法

注意:这只适用于iOS 9+,因为只有自定义的segues支持iOS9之前的类名称,并且不能在故事板中声明退出segue作为自定义segue

1. UIStoryboardSegue与UistoryboardSegueWi完成

 class UIStoryboardSegueWithCompletion: UIStoryboardSegue { var completion: (() -> Void)? override func perform() { super.perform() if let completion = completion { completion() } } } 

2.将UIStoryBoardSegueWithCompletion设置为退出时的类

注意:这个segue的动作应该是unwindToMainMenu来匹配原来的问题

从故事板中选择退出segue 添加自定义类

3.更新您的unwind @IBAction以在完成处理程序中执行代码

 @IBAction func unwindToMainMenu(segue: UIStoryboardSegue) { if let segue = segue as? UIStoryboardSegueWithCompletion { segue.completion = { self.performSegueWithIdentifier("Categories", sender: self) } } } 

您的代码现在将在退出segue完成其转换后执行

现在我想提供我自己的解决scheme来解决这个问题。 任何进一步的答案总是欢迎。

我把一个布尔variables和viewDidAppear函数给MainPageViewController

 var fromCamera = false override func viewDidAppear(animated: Bool) { if fromCamera { self.performSegueWithIdentifier("categorySelection", sender: self) self.fromCamera = false } } 

在我从CropViewController执行展开CropViewController之前,我将fromCamera设置为true 。 通过这种方式,只有在从裁剪视图中展开后退时才执行到类别屏幕的继续。

我猜这是因为退风赛还没有结束 我现在唯一能想到的是延迟使用dispatch_after调用performSegue。 这对我来说似乎很“黑客”。

 @IBAction func unwindToMainMenu(segue: UIStoryboardSegue) { dispatch_after(1, dispatch_get_main_queue()) { () -> Void in self.performSegueWithIdentifier("Categories", sender: self) } } 

拿着这个答案 (我只有Objective-C代码)

子类UIStoryBoardSegue

 #import <UIKit/UIKit.h> @interface MyStoryboardSegue : UIStoryboardSegue /** This block is called after completion of animations scheduled by @p self. */ @property (nonatomic, copy) void(^completion)(); @end 

并在完成animation后调用完成块。

 @implementation MyStoryboardSegue - (void)perform { [super perform]; if (self.completion != nil) { [self.destinationViewController.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) { if (![context isCancelled]) { self.completion(); } }]; } } @end 

退出segue IBAction方法发生在实际展开segue完成之前。 我有同样的问题,并通过这种方式解决(如果你不介意我的解释你的代码)。 它避免了依赖于ViewDidAppear的额外时间和animation。

 @IBAction func unwindToMainMenu(segue: UIStoryboardSegue) { let categoriesTable = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("CategoryTableViewController") self.navigationController?.viewControllers.append(categoriesTable) self.navigationController?.showViewController(categoriesTable, sender: self) } 

希望这对遇到这个问题的其他人有帮助,只是想要一个瞬间的转换!

回想前面的两个答案,这里有更多关于Objective C版本的细节(我也只有Objective-C代码)

  1. 子类UIStoryboard与UistoryboardSegueWithCompletion讨论

    class UIStoryboardSegueWithCompletion:UIStoryboardSegue {var completion:(() – > Void)?

     override func perform() { super.perform() if let completion = completion { completion() } } 

    }

UIStoryboardSegueWithCompletion.h

 #import <UIKit/UIKit.h> @interface MyStoryboardSegue : UIStoryboardSegueWithCompletion @property (nonatomic, copy) void(^completion)(); @end 

UIStoryboardSegueWithCompletion.m

 #import "UIStoryboardSegueWithCompletion.h" @implementation UIStoryboardSegueWithCompletion - (void)perform { [super perform]; if (self.completion != nil) { [self.destinationViewController.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) { if (![context isCancelled]) { self.completion(); } }]; } } @end 
  1. 将UIStoryBoardSegueWithCompletion设置为退出时的类

注意:这个segue的动作应该是unwindToMainMenu来匹配原来的问题[图片显示segue ui] [1] [显示segue ui 2的图片] [2]

从storyboardselect退出segue添加自定义类

 -(IBAction)unwindToMainMenu(UIStoryboardSegue *)segue { if([segue isKindOfClass:[UIStoryboardSegueWithCompletion class]]){ UIStoryboardSegueWithCompletion *segtemp = segue;// local prevents warning segtemp.completion = ^{ NSLog(@"segue completion"); [self performSegueWithIdentifier:@"Categories" sender:self]; }; } } 

您的代码现在将在退出segue完成其转换后执行