同一视图控制器出现两次

时间:2019-05-22 10:13:02

标签: ios swift

实际上,我想通过使用导航控制器将一个视图控制器移动到另一个,而另一个通过单击相应的按钮来固定。 我有两个视图控制器,一个是绿色视图控制器,另一个是红色视图控制器。 Storyboard上的序列是直接从名为“ toRedSegue”的视图控制器开始的。我在绿色视图控制器中有一个2按钮。当我单击按钮1时,红色视图控制器出现一次。如果我单击按钮2红色视图控制器出现两次。

以另一种方式,我在Storyboard上添加了segue,它直接从Button开始,结果相同。但是,如果我从情节提要中删除了segue,那就很好。

My question is why same view controller appear twice?

Here is the code snippet

GreenVC.Swift
=============
/* This view controller contain two IBAction, one for button 1 
which implement the performSeque with seque identifier. Another 
for button 2 which implement the view transition by pushing view 
controller in directly on navigation controller using 
pushViewController API. */

// IBAction for button 1
@IBAction func button1PerformSegueAction(_ sender: Any) {
    performSegue(withIdentifier: "toRedSegue", sender: self) // move green to red view controller
}

// IBAction for button 2
@IBAction func button2PerformPushVCtoNavigationContronller(_ sender: Any) {
    let myVC = storyboard?.instantiateViewController(withIdentifier: "RedVC") as! RedVC
    myVC.value = "hello view transition through push navigation controller"
    navigationController?.pushViewController(myVC, animated: true) // push the view controller to the navigation stack.
}
/* As per my understanding this prepare method will call when view 
transition occur by seque. But i have not use any seque identifier
on button 2 click. */
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    print("i am come from green view. now i am in red view")
    (segue.destination as! RedVC).value = "hello view transition through perform segue"
}    

RedVC.Swift
===========
var value : String?
override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = "Red View"
    print("\(value!)")
}

Console output:
==============
i am come from green view. now i am in red view
hello view transition through push navigation controller
hello view transition through perform segue

0 个答案:

没有答案