[英]Swift | Dismiss ViewController and Push other ViewController
解决了它:我忘了在VC1中声明tvx.selectionDelegate = self,这就是为什么它从未执行
我搜索了很长时间,并在线为我的问题提供了两个类似的答案: 这里
但是所有这些都可以先关闭然后再显示另一个视图,我想关闭另一个PUB,然后按下另一个ViewC。
我有3个ViewController:
VC1可以调用它:DownloadsViewController
VC2可以调用它:SelectActionController
VC3可以调用它:fileInfoView
VC1模态呈现VC2,然后VC2应该关闭,而VC1应该立即按下VC3。
我试过了:
VC2:
self.present(vx, animated: true, completion: nil)
并将PUSH动画放入完成{},但它崩溃了。
我尝试的下一件事情是创建一个委托,如果我按VC2上的一个按钮,它将调用VC1以关闭VC2并按下VC3。 像这儿:
VC2:
protocol PushViewControllerDelegate: class {
func pushViewController(_ controller: UIViewController)
}
class SelectActionController: UIViewController, UITableViewDelegate, UITableViewDataSource {
weak var delegate: PushViewControllerDelegate?
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
if self.delegate != nil {
self.delegate?.pushViewController(self)}
VC1:
func pushViewController(_ controller: UIViewController) {
controller.dismiss(animated: true) { () -> Void in
self.performSegue(withIdentifier: self.segue2, sender: nil)
//let vx = self.storyboard?.instantiateViewController(withIdentifier: "FileInfo") as! fileInfoView
//self.navigationController?.pushViewController(vx, animated: false)
}
}
我尝试过的Trird方法是创建一个全局变量,如果将其忽略,只需将Bool变量设置为true即可,并且VC1中的func应该可以识别它(我的意图)。 唯一的问题是它不会将其视为ViewDidAppear或类似的东西。
因此,这些都不起作用。
有人对此有想法吗?
OP评论后编辑
只需使用协议即可。 让您的第一个控制器采用它,并添加所需的功能。 在第二个控制器中设置一个协议变量,该控制器变量将第一个作为参考,并在您关闭第二个控制器时调用该函数。 现在,您可以使用dismiss函数执行所需的任何操作,传递数据,发送到其他控制器...
第一视图控制器:
protocol CustomProtocol {
func dismissed()
}
class AViewController: UIViewController, CustomProtocol {
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func toVC2(_ sender: UIButton) {
let VC = self.storyboard!.instantiateViewController(withIdentifier: "BViewController") as! BViewController
VC.customProtocol = self
VC.view.backgroundColor = .white
self.present(VC, animated: true, completion: nil)
}
func dismissed() {
let yourViewController = UIViewController()
yourViewController.view.backgroundColor = .red
guard let navigationController = self.navigationController else {return}
navigationController.pushViewController(yourViewController, animated: true)
}
}
第二个视图控制器:
class BViewController: UIViewController {
var customProtocol: CustomProtocol?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func quit(_ sender: UIButton) {
self.dismiss(animated: true) {
guard let proto = self.customProtocol else {return}
proto.dismissed()
}
}
}
我希望您需要对自己执行撤消操作
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
在“ SelectActionController”中对吗?
在这种情况下,您必须执行以下操作,
公共功能tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath){
if self.delegate != nil {
dismiss(animated: true) {
self.delegate?.pushViewController(self)}
}
}
我已经分享了一个与您的情况有关的示例项目,请参考。 https://github.com/Bharath-MallowTech/StackoverflowSolutions/tree/master/viewcontrollerDismissAndPush
您能发布一些您尝试过的代码吗? 这里的self
是什么? 是VC1
, VC2
还是VC3
?
您可以做的是看一下协议,因此您将VC1
用作delegate
并且在关闭VC2
也将其称为委托showVC3()
。 然后, delegate.showVC3()
您的delegate.showVC3()
放置在完成块中,以进行dismiss
。
// Declare it in VC1
@protocol MyDelegate {
func showVC3()
}
class VC1: UIViewController, MyDelegate {
var vc2: VC2ViewController = VC2ViewController()
var vc3: VC3ViewController = VC3ViewController()
func showVC2() {
vc2.delegate = self
self.present(self.vc2)
}
func showVC3() {
self.present(self.vc3)
}
}
// In VC2
class VC2: UIViewController {
var delegate: MyDelegate?
func closeVC2() {
self.dismiss(animated: true, completion: {
self.delegate?.showVC3()
})
}
}
不确定是否可行,因为我目前在电脑上,所以没有尝试过。 但是根据您的需要进行更改并进行测试。
根据您来自Github的文件,我认为您忘记声明了
tvx.selectionDelegate = self
在里面
func imagePressed
甚至我也有同样的情况,即在呈现视图控制器之后,我需要推送到其他视图控制器。 如果我们有vc1,vc2,vc3,我们从vc1中展示了vc2,那么在单击vc2的按钮时,我们需要推送到vc3,我所做的是,我将vc3设置为根视图控制器。 您可以按照以下方式检查代码。
func goToHomeScreenPage(transition : Bool)
{
let _navigation : UINavigationController!
let startVC : TabbarViewController = Utilities.viewController(name: "TabbarViewController", onStoryboard: StoryboardName.Login.rawValue) as! TabbarViewController
_navigation = UINavigationController(rootViewController: startVC)
_navigation.setNavigationBarHidden(true, animated: true)
let transitionOption = transition ? UIViewAnimationOptions.transitionFlipFromLeft : UIViewAnimationOptions.transitionFlipFromLeft
gotoViewController(viewController: _navigation, transition: transitionOption)
}
上面的方法gotoviewcontroller()在下面,
func gotoViewController(viewController: UIViewController, transition: UIViewAnimationOptions)
{
if transition != UIViewAnimationOptions.transitionCurlUp
{
UIView.transition(with: self.window!, duration: 0.5, options: transition, animations: { () -> Void in
self.window!.rootViewController = viewController
}, completion: { (finished: Bool) -> Void in
// do nothing
})
} else {
window!.rootViewController = viewController
}
}
声明var window: UIWindow?
最重要的是,您可以在appdelegate中编写这些方法,并通过创建appdelegate实例来调用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.