Passing Data between View Controllers in Swift

2019-01-02 18:52发布

I am trying to convert an app from Objective-C to Swift but I can't find how to pass data between views using Swift. My Objective-C code is

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
AnsViewController *ansViewController;
ansViewController = [storyBoard instantiateViewControllerWithIdentifier:@"ansView"];
ansViewController.num = theNum;
[self presentViewController:ansViewController animated:YES completion:nil];

What that is doing is it basically takes the variable, theNum, and passes it to the variable, num, on a different view controller. I know this may be an easy question but I am getting pretty confused with Swift so if someone could explain how they changed it to Swift that would be greatly appreciated!

Thanks

7条回答
长期被迫恋爱
2楼-- · 2019-01-02 18:52

To pass string or any data from one controller to another in swift.

Follows below steps:

1) Create property in child controller as var abc:string!

2) Create object of childcontroller

let storyboard:UIStoryboard()
let viewController: childcontroller = storyboard.instantiateViewControllerWithIdentifier("childcontroller") as! childcontroller
viewController.abc = "hello";
self.navigationController.pushviewController(Controller:viewController animated:true CompletionHandler:nil)
查看更多
大哥的爱人
3楼-- · 2019-01-02 18:54

To pass string or any data from one controller to another in swift.

Follows below steps:

1) Create variable of type which you want like (String,Int) var test : String!

2) Create object of childcontroller

 let vc = self.storyboard?.instantiateViewController(withIdentifier: "Here identifier of your VC") as! (Here Name Of Your Controller)
 vc.test = "Hello" (Or any data which you want to pass)
 self.navigationController?.pushViewController(VC, animated: true)

That should solve your problem

查看更多
旧时光的记忆
4楼-- · 2019-01-02 19:00
Using tableview,

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
        {
            let ClubProfileView = self.storyboard?.instantiateViewController(withIdentifier: "CBClubProfileViewController") as! CBClubProfileViewController

            let TempCulubDic:NSDictionary =
                ((ClubsListbyDateDic.object(forKey:((ClubsListbyDateDic.allKeys as! [String]).sorted(by: <)as NSArray).object(at: indexPath.section) as! String) as! NSArray).object(at: indexPath.row))as! NSDictionary

            let ClubId:String=(TempCulubDic.value(forKey: "club_id") as? String)!

            let CheckIndate:String=(TempCulubDic.value(forKey: "chekin_date") as? String)!

            ClubProfileView.ClubID=ClubId

            ClubProfileView.CheckInDate = CheckIndate

            // self.tabBarController?.tabBar.isHidden=true

            ClubProfileView.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(ClubProfileView, animated: true)
            }
查看更多
栀子花@的思念
5楼-- · 2019-01-02 19:05
class AnsViewController: UIViewController {
   var theNum: Int

    override func viewDidLoad() {
        super.viewDidLoad()

        println(theNum)
    }

}

override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    let viewController = self.storyboard.instantiateViewControllerWithIdentifier("ansView") as AnsViewController
    viewController.num = theNum
    self.presentViewController(viewController, animated: true, completion: nil)
}
查看更多
柔情千种
6楼-- · 2019-01-02 19:11

Let's assumed we stand at the firstView go to the DetailView and want passing data from firstView to Detailview. To do that with storyboard, at the firstView we will have a method:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "segueTest") {
      //Checking identifier is crucial as there might be multiple
      // segues attached to same view
      var detailVC = segue!.destinationViewController as DetailViewController;
      detailVC.toPass = textField.text
    }
}

and then into the class of DetailView we declared a variable:

var toPass: String!

then you can use the variable toPass (of course you can change the type of the variable as you want, in this EX I just demo for string type).

查看更多
谁念西风独自凉
7楼-- · 2019-01-02 19:11
     @IBAction func nextbtnpreesd(_ sender: Any) {

            let mystring = "2"
            performSegue(withIdentifier: "MusicVC", sender: mystring)
        }



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {


        if let destination = segue.destination as? MusicVC{

            if let song = sender as? String{
                destination.strlablevalie = song
            }
        }
    }

//in MusicVC create string like:

 var strlablevalie:String!
查看更多
登录 后发表回答