Ios 在UIPageViewController中以编程方式转到下一页
我有一个rootViewController,它基本上是我的pageViewController,它实现了所有的页面控制器协议功能。我已经用一个正确显示当前页码的pageControl成功地实现了整个pageViewController 在rootViewController中包含的第一个页面上,我有一个viewController,其中有多个文本字段必须用pickerView填充。我还创建了一个随选择器一起使用的inputAccessory工具栏。一旦所有字段都填充了文本,工具栏上的“完成”按钮将切换为“完成”。点击这个“完成”按钮,在这个viewController中,我希望rootViewController转到下一页 我第一次尝试直接显示我想要打开的viewController,但注意到页面没有作为pageViewController的一部分显示(很明显,为什么它现在不工作!)。因此,我随后尝试实现一个协议,该协议将触发rootViewController转到下一页。我已经完美地实现了一切,但是没有设置代理。我似乎不明白为什么没有设置。这可能是因为我在UIPageViewController框架流中遗漏了什么吗?我被难住了 最终在协议中,我尝试在包含的ViewController和rootViewController之间进行通信 以下是我的源代码: 在rootController中:Ios 在UIPageViewController中以编程方式转到下一页,ios,swift,protocols,uipageviewcontroller,uitoolbar,Ios,Swift,Protocols,Uipageviewcontroller,Uitoolbar,我有一个rootViewController,它基本上是我的pageViewController,它实现了所有的页面控制器协议功能。我已经用一个正确显示当前页码的pageControl成功地实现了整个pageViewController 在rootViewController中包含的第一个页面上,我有一个viewController,其中有多个文本字段必须用pickerView填充。我还创建了一个随选择器一起使用的inputAccessory工具栏。一旦所有字段都填充了文本,工具栏上的“完成”按
import UIKit
class rootPageViewController: UIViewController, UIPageViewControllerDataSource,
UIPageViewControllerDelegate, tutorialPageTurnerDelegate {
var tutorialOne = userInputViewController()
@IBOutlet var pageLight: UIPageControl!
var turn: UIPageViewController!
let pages = ["userInput","tutorialOne","tutorialTwo","connectScreen"]
var i: Int!
override func viewDidLoad() {
super.viewDidLoad()
if let take = storyboard?.instantiateViewControllerWithIdentifier("pageView") {
self.addChildViewController(take)
self.view.addSubview(take.view)
take.view.addSubview(pageLight)
take.view.bringSubviewToFront(pageLight)
turn = take as! UIPageViewController
turn.dataSource = self
turn.delegate = self
// the delegate for the protocol I've implemented
tutorialOne.delegate = self
turn.setViewControllers([viewControllerAtIndex(0)!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
turn.didMoveToParentViewController(self)
}
}
.
.
... pageviewcontroller protocol functions and misc code ...
.
.
// Helper Function to return Index
func viewControllerAtIndex(index: Int) -> UIViewController? {
let vc = storyboard?.instantiateViewControllerWithIdentifier(pages[index])
// _________EDIT_________
if index == 0 {
var tutorialOne: userInputViewController = userInputViewController()
tutorialOne.delegate = self
}
return vc
}
// function required by the protocol
func saveActionCompleted() {
print("Yes it does come here")
turn.setViewControllers([viewControllerAtIndex(1)!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
turn.didMoveToParentViewController(self)
}
...
下面是我创建协议的viewcontroller:
import UIKit
import CoreData
protocol tutorialPageTurnerDelegate {
func saveActionCompleted()
}
class userInputViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate,
UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var delegate: tutorialPageTurnerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
//Create toolbar
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 81/255, green: 45/255, blue: 168/255, alpha: 1)
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePicker")
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
toolBar.setItems([spaceButton, nextButton], animated: false)
toolBar.userInteractionEnabled = true
....... more misc code ...
}
// The selector function for the toolbar from where I want to notify the root controller
func donePicker(){
self.view.endEditing(true)
saveDataAction(self)
if delegate != nil {
delegate!.saveActionCompleted()
} else {
print("PageTurnerDelegate is nil")
}
}
...
} //End
我要换页了。门是零。我尝试了很多变通办法,但都没有成功。我确实觉得可能有更好的方法。欢迎提出任何建议或见解 您需要设置将显示的视图控制器实例的
委托
属性:
func viewControllerAtIndex(index: Int) -> UIViewController? {
let vc = storyboard?.instantiateViewControllerWithIdentifier(pages[index])
if index == 0 {
let userInputViewController =vc as! userInputViewController
userInputViewController.delegate = self
}
return vc
}
此外,按照惯例,类以大写字母开头,因此它应该是
UserInputViewController
您需要设置将显示的视图控制器实例的委托
属性:
func viewControllerAtIndex(index: Int) -> UIViewController? {
let vc = storyboard?.instantiateViewControllerWithIdentifier(pages[index])
if index == 0 {
let userInputViewController =vc as! userInputViewController
userInputViewController.delegate = self
}
return vc
}
另外,按照惯例,类以大写字母开头,因此应该是
UserInputViewController
,我看不出您在哪里将tutorialOne变量设置为第一个视图控制器。你能显示viewControllerAtIndex
吗?我已经编辑好了。您可以在rootViewController代码段中看到viewControllerAtIndex
,因此,在该方法中,您正在实例化一个新的视图控制器,但从未设置委托。因此,您有一个视图控制器实例tutorially
,其中设置了代理,但从未显示此实例,然后,您将看到由viewControllerAtIndex
返回的其他视图控制器实例,其中您从未设置过委托。好的,因此我进入了viewControllerAtIndex
函数,并尝试在其中单独为tutorial设置委托,但它仍然没有被设置。你能详细解释一下你所说的“你从来没有显示过这个实例”是什么意思吗?我的意思是你分配了一个userInputViewController
的实例,并把它放在tutorialOne
中,但你从来没有真正显示tutorialOne
。在viewControllerAtIndex
中设置代理应该可以工作。我看不到将tutorialOne变量设置为第一个视图控制器的位置。你能显示viewControllerAtIndex
吗?我已经编辑好了。您可以在rootViewController代码段中看到viewControllerAtIndex
,因此,在该方法中,您正在实例化一个新的视图控制器,但从未设置委托。因此,您有一个视图控制器实例tutorially
,其中设置了代理,但从未显示此实例,然后,您将看到由viewControllerAtIndex
返回的其他视图控制器实例,其中您从未设置过委托。好的,因此我进入了viewControllerAtIndex
函数,并尝试在其中单独为tutorial设置委托,但它仍然没有被设置。你能详细解释一下你所说的“你从来没有显示过这个实例”是什么意思吗?我的意思是你分配了一个userInputViewController
的实例,并把它放在tutorialOne
中,但你从来没有真正显示tutorialOne
。在viewControllerAtIndex
中设置代理应该有效,我会记住这一点。非常感谢你的建议。协议现在生效了,我会记住的。非常感谢你的建议。协议现在正在生效。