将变量从页面视图控制器传递到另一个
Posted
技术标签:
【中文标题】将变量从页面视图控制器传递到另一个【英文标题】:Passing variables from page view controller to another 【发布时间】:2018-12-21 20:58:12 【问题描述】:我正在尝试使用UIPageViewController
制作注册表单,因此我正在尝试将变量从子视图传递到另一个视图。
这是我的页面视图控制器:
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.
*/
我的主要目标是将用户名从第一个孩子传递给第二个孩子,这样我就可以使用它来使用网络服务 这些是我的故事板和我的注册表 registration form
storyboard
我试图在第一页上获取用户名、密码和电子邮件,然后将用户名传递到下一个子视图,以便我可以将金额发送到数据库
【问题讨论】:
您可以使用prepare for segue在VC之间传递数据 我问,是否有另一种方法来传递变量扔 pageviewcontroller 孩子 【参考方案1】:您需要使用以下代码
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 Identifiers 设置的。 如果您有多个标识符,只需继续 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
【讨论】:
问题是不想使用 segues 因为,我使用 pageviewcontroller 所以我滚动扔孩子我没有执行一个动作(点击按钮...) 感谢您的回复,但它与我的情况不兼容。我只是将表单更改为一个 uiviewcontroller 我对你的特殊情况很好奇。如果您可以分享该代码,我将非常感激!以上是关于将变量从页面视图控制器传递到另一个的主要内容,如果未能解决你的问题,请参考以下文章
将数据从带有页面控制器的 uiscrollview 传递到另一个视图控制器 [关闭]
Extjs 7,如何使用视图模型将动态数据从一个控制器传递到另一个控制器
Rails 将@variable 从一个显示页面传递到另一个显示页面