将变量从页面视图控制器传递到另一个
Passing variables from page view controller to another
我正在尝试使用 UIPageViewController
制作注册表单,因此我正在尝试将变量从 child 视图传递到另一个视图。
这是我的页面视图控制器:
class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
var pageControl = UIPageControl()
// MARK: UIPageViewControllerDataSource
lazy var orderedViewControllers: [UIViewController] = {
return [self.newVc(viewController: "First"),
self.newVc(viewController: "Second"),
self.newVc(viewController: "Third")]
}()
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
// This sets up the first view that will show up on our page control
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .forward,
animated: true,
completion: nil)
}
configurePageControl()
// Do any additional setup after loading the view.
}
func configurePageControl() {
// The total number of pages that are available is based on how many available colors we have.
pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 50,width: UIScreen.main.bounds.width,height: 50))
self.pageControl.numberOfPages = orderedViewControllers.count
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.black
self.pageControl.pageIndicatorTintColor = UIColor.white
self.pageControl.currentPageIndicatorTintColor = UIColor.black
self.view.addSubview(pageControl)
}
func newVc(viewController: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController)
}
// MARK: Delegate methords
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
let pageContentViewController = pageViewController.viewControllers![0]
self.pageControl.currentPage = orderedViewControllers.index(of: pageContentViewController)!
}
// MARK: Data source functions.
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
// User is on the first view controller and swiped left to loop to
// the last view controller.
guard previousIndex >= 0 else {
return orderedViewControllers.last
// Uncommment the line below, remove the line above if you don't want the page control to loop.
// return nil
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
// User is on the last view controller and swiped right to loop to
// the first view controller.
guard orderedViewControllersCount != nextIndex else {
return orderedViewControllers.first
// Uncommment the line below, remove the line above if you don't want the page control to loop.
// return nil
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
这是我的注册控制器
class RegisterViewController: UIViewController {
var username = ""
var email = ""
var password = ""
var money = ""
var usn = ""
@IBOutlet weak var textUsername: UITextField!
@IBOutlet weak var textEmail: UITextField!
@IBOutlet weak var textPassword: UITextField!
@IBOutlet weak var textMoney: UITextField!
@IBOutlet weak var labelUsername: UILabel!
@IBAction func btnValidate(_ sender: Any) {
username = textUsername.text!
usn = username
email = textEmail.text!
password = textPassword.text!
print (username)
// labelUsername.text = username
// API.register(username: username, email: email, password: password)
}
@IBAction func btnSaveMoney(_ sender: Any) {
money = String(textMoney.text!)
print(money)
print(username)
}
@IBAction func btnBegin(_ sender: Any) {
// API.setMoney(money: money, username: username)
// print(textMoney.text!)
print("your name"+usn)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
我的主要目标是将用户名从第一个 child 传递给第二个,这样我就可以用它来使用网络服务
这些是我的故事板和我的注册表
registration form
storyboard
我试图在第一页上获取用户名、密码和电子邮件,然后将用户名传递到下一个 child 视图,这样我就可以将金额发送到数据库
您需要使用以下代码
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourSegueIdentiferHere" {
if let sendData = segue.destination as? destinationVC
//All the data you want to send to viewController
}
}
}
此示例是为多个 segue 标识符设置的。
如果您有多个标识符,只需继续 if 语句
编辑:
如果你不想使用 segues...
也许这对你有用?
override func viewDidLoad() {
super.viewDidLoad()
self.delegate=self
self.dataSource=self
let childViewController=storyboard?.instantiateViewController(withIdentifier: "some identifier of view") as! ChildViewController
if let variable = dataToPass {
print(variable)
childViewController.variable = dataToPass
}
我正在尝试使用 UIPageViewController
制作注册表单,因此我正在尝试将变量从 child 视图传递到另一个视图。
这是我的页面视图控制器:
class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
var pageControl = UIPageControl()
// MARK: UIPageViewControllerDataSource
lazy var orderedViewControllers: [UIViewController] = {
return [self.newVc(viewController: "First"),
self.newVc(viewController: "Second"),
self.newVc(viewController: "Third")]
}()
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
// This sets up the first view that will show up on our page control
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .forward,
animated: true,
completion: nil)
}
configurePageControl()
// Do any additional setup after loading the view.
}
func configurePageControl() {
// The total number of pages that are available is based on how many available colors we have.
pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 50,width: UIScreen.main.bounds.width,height: 50))
self.pageControl.numberOfPages = orderedViewControllers.count
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.black
self.pageControl.pageIndicatorTintColor = UIColor.white
self.pageControl.currentPageIndicatorTintColor = UIColor.black
self.view.addSubview(pageControl)
}
func newVc(viewController: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController)
}
// MARK: Delegate methords
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
let pageContentViewController = pageViewController.viewControllers![0]
self.pageControl.currentPage = orderedViewControllers.index(of: pageContentViewController)!
}
// MARK: Data source functions.
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
// User is on the first view controller and swiped left to loop to
// the last view controller.
guard previousIndex >= 0 else {
return orderedViewControllers.last
// Uncommment the line below, remove the line above if you don't want the page control to loop.
// return nil
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
// User is on the last view controller and swiped right to loop to
// the first view controller.
guard orderedViewControllersCount != nextIndex else {
return orderedViewControllers.first
// Uncommment the line below, remove the line above if you don't want the page control to loop.
// return nil
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
这是我的注册控制器
class RegisterViewController: UIViewController {
var username = ""
var email = ""
var password = ""
var money = ""
var usn = ""
@IBOutlet weak var textUsername: UITextField!
@IBOutlet weak var textEmail: UITextField!
@IBOutlet weak var textPassword: UITextField!
@IBOutlet weak var textMoney: UITextField!
@IBOutlet weak var labelUsername: UILabel!
@IBAction func btnValidate(_ sender: Any) {
username = textUsername.text!
usn = username
email = textEmail.text!
password = textPassword.text!
print (username)
// labelUsername.text = username
// API.register(username: username, email: email, password: password)
}
@IBAction func btnSaveMoney(_ sender: Any) {
money = String(textMoney.text!)
print(money)
print(username)
}
@IBAction func btnBegin(_ sender: Any) {
// API.setMoney(money: money, username: username)
// print(textMoney.text!)
print("your name"+usn)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
我的主要目标是将用户名从第一个 child 传递给第二个,这样我就可以用它来使用网络服务 这些是我的故事板和我的注册表 registration form
storyboard
我试图在第一页上获取用户名、密码和电子邮件,然后将用户名传递到下一个 child 视图,这样我就可以将金额发送到数据库
您需要使用以下代码
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourSegueIdentiferHere" {
if let sendData = segue.destination as? destinationVC
//All the data you want to send to viewController
}
}
}
此示例是为多个 segue 标识符设置的。 如果您有多个标识符,只需继续 if 语句
编辑: 如果你不想使用 segues... 也许这对你有用?
override func viewDidLoad() {
super.viewDidLoad()
self.delegate=self
self.dataSource=self
let childViewController=storyboard?.instantiateViewController(withIdentifier: "some identifier of view") as! ChildViewController
if let variable = dataToPass {
print(variable)
childViewController.variable = dataToPass
}