Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在swift中的ViewController之间发送信息_Ios_Swift_Uiviewcontroller_Segue - Fatal编程技术网

Ios 在swift中的ViewController之间发送信息

Ios 在swift中的ViewController之间发送信息,ios,swift,uiviewcontroller,segue,Ios,Swift,Uiviewcontroller,Segue,我有两门课: class ExplorerViewController: UIViewController { lazy var studyButton: ExploreButton = { let button = ExploreButton() button.setTitle("Study", forState: .Normal) return button }() } 及 我正试图使其成为这样,当我单击studyButt

我有两门课:

class ExplorerViewController: UIViewController {

    lazy var studyButton: ExploreButton = {
        let button = ExploreButton()
        button.setTitle("Study", forState: .Normal)
        return button
    }()

}

我正试图使其成为这样,当我单击studyButton时,它会将按钮标题发送到ViewController并转到该视图

我没有使用故事板,我在使用segues时遇到了麻烦,因为每个教程似乎都给出了不同的示例,这些示例都是针对他们一直在使用的东西的,而且95%的示例似乎都是使用故事板操作的。有人能给我一个大概的方法吗


我如何给起始视图控制器一个标识符,因为它不像我“移动”到的其他控制器那样实例化。如何从ViewController移动到ExplorerViewController,然后再移回同一个ViewController(所有更改保持不变)

为接收“title”变量的ViewController创建初始值设定项:

class ViewController: UIViewController, UISearchBarDelegate, LocateOnTheMap, GMSMapViewDelegate {
    var btnTitle: String?

    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?, btnTitle:String?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        self.btnTitle = btnTitle
    }
}
创建ViewController对象时,请使用此初始值设定项

var viewController = ViewController(nibName: "ViewController", bundle: nil, btnTitle: title

您可以初始化要导航到的UIViewController,将数据分配给该控制器中的属性,并调用此方法:

presentViewController(viewController, animated: true, completion: nil)
例如:

let destinationViewController = ViewController()
destinationViewController.frame = self.view.frame
destinationViewController.buttonTitle = "title"
self.presentViewController(destinationViewController, animated: true, completion: nil)
尽管我建议您熟悉故事板并使用Segues执行导航。

请查看此问题,了解如何在视图之间切换


要在引用新视图后传递数据,可以将数据分配给该视图的属性。

请确保以下两件事:-

1.)您已经为您的
viewController
提供了一个情节提要ID让我们在它的标识检查器中说
“viewControllerVC\u ID”

2.)您已将
NavigationController
嵌入到初始入口点视图控制器中

ViewController
中声明一个变量

var btnLabelTxt : String!
explorervicewcontroller
中创建该按钮的
@IBAction

@IBAction func exploreBtnAction(sender : UIButton!){

        let vcScene = self.navigationController?.storyboard?.instantiateViewControllerWithIdentifier("viewControllerVC_ID") as! ViewController

        vcScene.btnLabelTxt = "Study"

        //or you can just access the button itself in the viewController and set the title
        //By vcScene.yourBtn.setTitle("Study", forState: .Normal)   

        self.navigationController?.pushViewController(vcScene, animated: true)   


}

谢谢你的回答。是否需要init(nibName)etc?谢谢您的回答。这个答案让我可以从explorerViewController移动到viewController。有没有办法从viewController移回该ExplorerServiceController实例?是的,您可以使用名为
dismissViewControllerAnimated
的方法,例如:
self.dismissViewControllerAnimated(true,completion:nil)
@IBAction func exploreBtnAction(sender : UIButton!){

        let vcScene = self.navigationController?.storyboard?.instantiateViewControllerWithIdentifier("viewControllerVC_ID") as! ViewController

        vcScene.btnLabelTxt = "Study"

        //or you can just access the button itself in the viewController and set the title
        //By vcScene.yourBtn.setTitle("Study", forState: .Normal)   

        self.navigationController?.pushViewController(vcScene, animated: true)   


}