是)我有的
我有一个ViewController(TutorialViewController)和一个UIPageViewController(TutorialPageViewController)。带有StoryBoard ID的 Storyboard 中还有3个额外的 View :
我一直在关注这个tutorial(对作者表示敬意,写得很好)。
在Green View Controller上,我定义了一个变量:
var passedVariable = ""
然后在ViewDidLoad中将其打印出来。
这是两个具有代码的 Controller :
UIViewController(TutorialViewController):
class TutorialViewController: UIViewController {
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var containerView: UIView!
var tutorialPageViewController: TutorialPageViewController? {
didSet {
tutorialPageViewController?.tutorialDelegate = self
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let tutorialPageViewController = segue.destinationViewController as? TutorialPageViewController {
self.tutorialPageViewController = tutorialPageViewController
}
}
@IBAction func didTapNextButton(sender: UIButton) {
tutorialPageViewController?.scrollToNextViewController()
}
}
extension TutorialViewController: TutorialPageViewControllerDelegate {
func tutorialPageViewController(tutorialPageViewController: TutorialPageViewController,
didUpdatePageCount count: Int) {
pageControl.numberOfPages = count
}
func tutorialPageViewController(tutorialPageViewController: TutorialPageViewController,
didUpdatePageIndex index: Int) {
pageControl.currentPage = index
}
}
UIPageViewController
class TutorialPageViewController: UIPageViewController {
weak var tutorialDelegate: TutorialPageViewControllerDelegate?
//let vc0 = GreenViewController(nibName: "GreenViewController", bundle: nil)
private(set) lazy var orderedViewControllers: [UIViewController] = {
// The view controllers will be shown in this order
return [self.newColoredViewController("Green"),
self.newColoredViewController("Red"),
self.newColoredViewController("Blue"), self.newColoredViewController("Pink")]
}()
override func viewDidLoad() {
super.viewDidLoad()
//self.vc0.passedVariable = "Passed Data"
dataSource = self
delegate = self
if let initialViewController = orderedViewControllers.first {
scrollToViewController(initialViewController)
}
tutorialDelegate?.tutorialPageViewController(self,
didUpdatePageCount: orderedViewControllers.count)
}
/**
Scrolls to the next view controller.
*/
func scrollToNextViewController() {
if let visibleViewController = viewControllers?.first,
let nextViewController = pageViewController(self,
viewControllerAfterViewController: visibleViewController) {
scrollToViewController(nextViewController)
}
}
private func newColoredViewController(color: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewControllerWithIdentifier("\(color)ViewController")
}
/**
Scrolls to the given 'viewController' page.
- parameter viewController: the view controller to show.
*/
private func scrollToViewController(viewController: UIViewController) {
setViewControllers([viewController],
direction: .Forward,
animated: true,
completion: { (finished) -> Void in
// Setting the view controller programmatically does not fire
// any delegate methods, so we have to manually notify the
// 'tutorialDelegate' of the new index.
self.notifyTutorialDelegateOfNewIndex()
})
}
/**
Notifies '_tutorialDelegate' that the current page index was updated.
*/
private func notifyTutorialDelegateOfNewIndex() {
if let firstViewController = viewControllers?.first,
let index = orderedViewControllers.indexOf(firstViewController) {
tutorialDelegate?.tutorialPageViewController(self,
didUpdatePageIndex: index)
}
}
}
// MARK: UIPageViewControllerDataSource
extension TutorialPageViewController: UIPageViewControllerDataSource {
func pageViewController(pageViewController: UIPageViewController,
viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(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
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(pageViewController: UIPageViewController,
viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(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
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
}
extension TutorialPageViewController: UIPageViewControllerDelegate {
func pageViewController(pageViewController: UIPageViewController,
didFinishAnimating finished: Bool,
previousViewControllers: [UIViewController],
transitionCompleted completed: Bool) {
notifyTutorialDelegateOfNewIndex()
}
}
protocol TutorialPageViewControllerDelegate: class {
/**
Called when the number of pages is updated.
- parameter tutorialPageViewController: the TutorialPageViewController instance
- parameter count: the total number of pages.
*/
func tutorialPageViewController(tutorialPageViewController: TutorialPageViewController,
didUpdatePageCount count: Int)
/**
Called when the current index is updated.
- parameter tutorialPageViewController: the TutorialPageViewController instance
- parameter index: the index of the currently visible page.
*/
func tutorialPageViewController(tutorialPageViewController: TutorialPageViewController,
didUpdatePageIndex index: Int)
}
我尝试过的
我试过像这样先声明 View Controller :
let vc0 = GreenViewController(nibName: "GreenViewController", bundle: nil)
然后像这样传递数据:
override func viewDidLoad() {
vc0.passedVariable = "This was passed, Dance with Joy"
}
控制台中没有任何输出。
我还尝试将上面的捆绑包更改为:
bundle: NSBundle.mainBundle()
仍然纳达
问题
我打算从alamofire请求中将数据加载到TutorialViewController上,我想将该数据传递给其中一个ViewController(绿色,蓝色,红色)
如何将已经从TutorialViewController中获取的数据传递给将要加载的 subview 之一?
最佳答案
首先,我要感谢您检查我的教程以及您所说的所有美好内容。
其次,我为您提供解决方案!我继续进行操作,并在教程中链接了committed the solution to the GitHub repo。我还将在此处发布代码。
(1)创建一个UIViewController
子类以添加自定义属性。在此示例中,我选择添加UILabel
,因为它是运行应用程序时最容易查看的代码。
class ColoredViewController: UIViewController {
@IBOutlet weak var label: UILabel!
}
(2)在
Main.storyboard
内部,将每个UIViewController
“页面”的自定义类更改为Identity Inspector中的ColoredViewController
。(3)在每个“页面”上添加一个
UILabel
,并根据需要对其进行约束。我选择将其在容器中垂直和水平居中。不要忘记将UILabel
链接到ColoredViewController
的@IBOutlet weak var label: UILabel!
。(4)可选:如果我们从不在代码中设置标签的文本,则以这种方式删除了每个默认的“标签”文本,因此不会向用户显示“标签”。
(5)我们需要对
TutorialPageViewController
做一些TLC,以便知道orderedViewControllers
现在是ColoredViewController
数组。为了简单起见,我将粘贴整个类:class TutorialPageViewController: UIPageViewController {
weak var tutorialDelegate: TutorialPageViewControllerDelegate?
private(set) lazy var orderedViewControllers: [ColoredViewController] = {
// The view controllers will be shown in this order
return [self.newColoredViewController("Green"),
self.newColoredViewController("Red"),
self.newColoredViewController("Blue")]
}()
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
delegate = self
if let initialViewController = orderedViewControllers.first {
scrollToViewController(initialViewController)
}
tutorialDelegate?.tutorialPageViewController(self,
didUpdatePageCount: orderedViewControllers.count)
}
/**
Scrolls to the next view controller.
*/
func scrollToNextViewController() {
if let visibleViewController = viewControllers?.first,
let nextViewController = pageViewController(self,
viewControllerAfterViewController: visibleViewController) {
scrollToViewController(nextViewController)
}
}
private func newColoredViewController(color: String) -> ColoredViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewControllerWithIdentifier("\(color)ViewController") as! ColoredViewController
}
/**
Scrolls to the given 'viewController' page.
- parameter viewController: the view controller to show.
*/
private func scrollToViewController(viewController: UIViewController) {
setViewControllers([viewController],
direction: .Forward,
animated: true,
completion: { (finished) -> Void in
// Setting the view controller programmatically does not fire
// any delegate methods, so we have to manually notify the
// 'tutorialDelegate' of the new index.
self.notifyTutorialDelegateOfNewIndex()
})
}
/**
Notifies '_tutorialDelegate' that the current page index was updated.
*/
private func notifyTutorialDelegateOfNewIndex() {
if let firstViewController = viewControllers?.first as? ColoredViewController,
let index = orderedViewControllers.indexOf(firstViewController) {
tutorialDelegate?.tutorialPageViewController(self,
didUpdatePageIndex: index)
}
}
}
// MARK: UIPageViewControllerDataSource
extension TutorialPageViewController: UIPageViewControllerDataSource {
func pageViewController(pageViewController: UIPageViewController,
viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard let coloredViewController = viewController as? ColoredViewController,
let viewControllerIndex = orderedViewControllers.indexOf(coloredViewController) 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
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(pageViewController: UIPageViewController,
viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let coloredViewController = viewController as? ColoredViewController,
let viewControllerIndex = orderedViewControllers.indexOf(coloredViewController) 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
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
}
extension TutorialPageViewController: UIPageViewControllerDelegate {
func pageViewController(pageViewController: UIPageViewController,
didFinishAnimating finished: Bool,
previousViewControllers: [UIViewController],
transitionCompleted completed: Bool) {
notifyTutorialDelegateOfNewIndex()
}
}
protocol TutorialPageViewControllerDelegate: class {
/**
Called when the number of pages is updated.
- parameter tutorialPageViewController: the TutorialPageViewController instance
- parameter count: the total number of pages.
*/
func tutorialPageViewController(tutorialPageViewController: TutorialPageViewController,
didUpdatePageCount count: Int)
/**
Called when the current index is updated.
- parameter tutorialPageViewController: the TutorialPageViewController instance
- parameter index: the index of the currently visible page.
*/
func tutorialPageViewController(tutorialPageViewController: TutorialPageViewController,
didUpdatePageIndex index: Int)
}
(6)在
TutorialViewController
内部:让我们设置label.text
。我选择使用viewDidLoad
,但是可以将此逻辑填充到网络请求完成块中。override func viewDidLoad() {
super.viewDidLoad()
if let greenColoredViewController = tutorialPageViewController?.orderedViewControllers.first {
greenColoredViewController.label.text = "Hello world!"
}
}
希望这可以帮助!
关于ios - PageViewController-将变量传递给 subview ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35681568/