如何使用常见的ViewController获取以前的ViewController的值?

时间:2019-07-10 08:24:20

标签: ios swift

在我的应用中,

我有一个CommonViewController,它提供了其他要使用的ViewController。

如果MyViewController推送到CommonViewController

在我的CommonViewController中,如何获取MyViewController值?

我知道我可以将数据从MyVC传递到CommonVC

但是这种情况CommonVC很常见

这种方式可能导致CommonVC有很多变量

这是MyViewController

class MyViewController:UIViewController {
var a = String()
var b = String()
var c = String()

//In this VC ,I use textField to save user typed result 
  a = "123" b = "456" c = "789"
//And push to CommonVC

}

这是CommonViewController

class CommonViewController:UIViewcontroller {

@IBAction func enterBtnPressAction(_ sender: Any){
  //I need to use MyVC's value to do something 
 }

}

我的大脑被CommonVC概念吸住了

每个VC都有机会使用

按钮功能enterBtnPressAction

需要处理各种情况

如果这两个VC仅允许我使用

我可以轻松地将值从A传递给B

但是CommonVC需要从其他人那里使用

CommonVC似乎具有MyVC的a,b,c变量

有一天CommonVC还有其他VC的变量

有更好的方法来解决这种情况吗?

我真的很新,现在很擅长询问是否需要更多信息,谢谢。

3 个答案:

答案 0 :(得分:0)

您必须使用继承概念,例如CommonVC应该是访问Common VC所需的所有vc的超超类。

示例

class CommonViewController:UIViewcontroller {

@IBAction func enterBtnPressAction(_ sender: Any){
  //I need to use MyVC's value to do something 
 }

}

MyViewController类通过重写使用常见的vc enterBtnPress操作

class MyViewController:CommonViewController {
var a = String()
var b = String()
var c = String()

//In this VC ,I use textField to save user typed result 
  a = "123" b = "456" c = "789"
//And push to CommonVC
override func enterBtnPressAction(_ sender: Any){
      //You can use MyVC's value to do something 
     }

}

Class AnotherVC通过重写使用常见的vc enterBtnPress操作

class AnotherController:CommonViewController {
    var p = String()
    var q = String()
    var r = String()

    //In this VC ,I use textField to save user typed result 
      p = "123" q = "456" r = "789"
    //And push to CommonVC
    override func enterBtnPressAction(_ sender: Any){
          //You can use AnotherVC value to do something 
         }

    }

答案 1 :(得分:0)

要使用Main VC中commonView控制器中的值,您应该使用故事板标识符将mainVC中的值传递给Common VC。

您应该在MyViewController的按钮单击中编写这段代码:

let myVC = self.storyboard?.instantiateViewController(withIdentifier: "identifier of commonviewcontroller declared in main storyboard") as! CommonViewController
                myVC.avalue = a
                myVC.bvalue = b
                myVC.cvalue = c
                self.navigationController?.pushViewController(myVC, animated: true)

接下来,在CommonViewController中,您应该声明变量以存储从My View COntroller传递的值:

    class CommonViewController: UIViewController{

        var avalue:String = ""
        var bvalue:String = ""
        var cvalue:String = ""
        override func viewDidLoad() {
            super.viewDidLoad()


        }
      @IBAction func enterBtnPressAction(_ sender: Any){

      // Now you can use the values here.

   }
}

答案 2 :(得分:0)

class MyViewController:UIViewController {
    var a = String()
    var b = String()
    var c = String()

    //In this VC ,I use textField to save user typed result

    override func viewDidLoad() {
        super.viewDidLoad()
        a = "123"
        b = "456"
        c = "789"
    }
    //And push to CommonVC
    @IBAction func pushToCommonVC(_ sender: Any){
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        // “common-VC” you need to set this identifier from storyboard select your viewController and set from inpector right side menu
        let commonVC = storyBoard.instantiateViewController(withIdentifier: "common-VC") as! commonVC
        commonVC.a = a
        commonVC.b = b
        commonVC.c = c
        self.navigationController?.pushViewController(commonVC, animated: true)
    }

}
class CommonViewController:UIViewcontroller {
    var a = String()
    var b = String()
    var c = String()
    override func viewDidLoad() {
        super.viewDidLoad()
        //values will print here
        print("\(a)")
        print("\(b)")
        print("\(c)")
    }
    @IBAction func enterBtnPressAction(_ sender: Any){
        //I need to use MyVC's value to do something
        //here you can use your a,b,c values
    }
}