在Swift中在视图控制器之间传递数据
我想转换一个应用程序从Objective-C到Swift,但我找不到如何使用Swift在视图之间传递数据。 我的Objective-C代码是
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; AnsViewController *ansViewController; ansViewController = [storyBoard instantiateViewControllerWithIdentifier:@"ansView"]; ansViewController.num = theNum; [self presentViewController:ansViewController animated:YES completion:nil];
这样做基本上是把variables,theNum,并把它传递给variablesnum,在不同的视图控制器上。 我知道这可能是一个简单的问题,但我对Swift很困惑,所以如果有人可以解释他们如何将其改变为Swift,将不胜感激!
谢谢
我们假设我们站在第一个视图去到DetailView,并希望将数据从firstView传递到Detailview。 要做到这一点与故事板,在第一个视图,我们将有一个方法:
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 } }
然后进入DetailView类,我们声明了一个variables:
var toPass: String!
那么你可以使用variablestoPass
(当然你可以根据需要更改variables的types,在这个EX中,我只是演示了stringtypes)。
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) }
将string或任何数据从一个控制器传递到另一个控制器。
遵循以下步骤:
1)在子控制器中创build属性为var abc:string!
2)创buildchildcontroller的对象
let storyboard:UIStoryboard() let viewController: childcontroller = storyboard.instantiateViewControllerWithIdentifier("childcontroller") as! childcontroller viewController.abc = "hello"; self.navigationController.pushviewController(Controller:viewController animated:true CompletionHandler:nil)
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) }
@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 } } }
//在MusicVC中创buildstring,如:
var strlablevalie:String!
注意:如果我们正在使用故事板
步骤1:主控制器:
// table row which row was selected func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!) { tableView.deselectRowAtIndexPath(indexPath, animated: true) println("You selected cell #\(indexPath.row)!") nextScreenRow = indexPath.row // get to the next screen self.performSegueWithIdentifier("dashboard_static_screen_segue", sender: self) }
接着;
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { if (segue.identifier == "dashboard_static_screen_segue") { var detailController = segue.destinationViewController as StaticScreens; detailController.screenNumber = nextScreenRow } }// end prepareForSegue
第2步:细节控制器(StaticScreen)
// set variable into your detail controller var screenNumber: NSInteger? println("selected row \(screenNumber!)")