2017-06-22 76 views
0

我有2个视图控制器AB。视图控制器A通过segue show呈现视图控制器BViewController B也有一个按钮,可以解除B并显示A。这里没有问题。如何从弹出视图中关闭第二个视图控制器

B中完成一些功能后,视图控制器B呈现一个弹出视图,此弹出包括重新启动游戏和关闭游戏按钮。当按下关闭游戏按钮时,视图控制器B和弹出视图应该被取消,并显示主视图控制器A。如何做到这一点?谢谢

这里如何呈现弹出观点:

let popupVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popupID") as! PopupViewController 
     self.addChildViewController(popupVC) 
     popupVC.view.frame = self.view.frame 
     self.view.addSubview(popupVC.view) 
     popupVC.didMove(toParentViewController: self) 
+0

可能重复的[IOS - 如何使用swift以编程方式进行分析](https://stackoverflow.com/questions/27604192/ios-how-to-segue-programmatically-using-swift) – Henry

+0

添加您的任何尝试代码和事物已经做到了。 –

+0

您是否正在尝试使用iPad或iPhone?如果ipad弹出视图意味着弹出控制器使用? iPhone和iPad均为 –

回答

2

在这里,Swift3的解决方案。

所以当我知道你想驳回的ViewController B中弹出并返回到视图控制器A.

let alertController = UIAlertController(title: "alert", message: "tara", preferredStyle: .alert) 

    let action = UIAlertAction(title: "dismiss", style: .default) { (UIAlertAction) in 

     // For Dismissing the Popup 
     self.dismiss(animated: true, completion: nil) 

     // Dismiss current Viewcontroller and back to ViewController B 
     self.navigationController?.popViewController(animated: true) 

    } 
    alertController.addAction(action) 
    self.present(alertController, animated: true, completion: nil) 
+0

非常感谢。这工作。 –

+0

对你有好处。没关系。 –

1

传统的方法来实现,这是使用委派。你的情况,你必须首先创建一个协议为代表

protocol PopupViewControllerDelegate { 
    func didSelectClose(_ popupVC: PopupViewController) 
} 

现在里面添加PopupViewController一个变量,用来呼叫委托的方法

class PopupViewController: UIViewController { 
    var delegate: PopupViewControllerDelegate? 
} 

当用户点击close按钮,您应该调用委托方法来通知用户的操作。

func didClose(sender: Any) { 
    delegate?.didSelectClose(self) 
} 

现在,你将必须实现内部ViewControllerB PopupViewControllerDelegate如下:

class ViewControllerB: UIViewController, PopupViewControllerDelegate { 
    func didSelectClose(_ popupVC: PopupViewController) { 
     // dismiss and go to Root View Controller (A) 
     dismiss(animated: true) { 
      self.navigationController?.popToRootViewController(animated: true) 
     } 
    } 
} 

正如你看到的,当didSelectClose被调用时,我们关闭该弹出式和弹出导航堆栈去ViewControllerA

最后,你目前PopupVC之前,你必须设置ViewControllerB作为委托

func showPopup() { 
    let popupVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popupID") as! PopupViewController 

    popupVC.delegate = self 
    present(popupVC, animated: true, completion: nil) 
} 
相关问题