簡體   English   中英

斯威夫特| 關閉ViewController並推送其他ViewController

[英]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是什么? VC1VC2還是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM