我使用uipageViewController来滑动页面。每个页面都有自己的ViewController:ContentViewController。在其中一个页面我有按钮。当用户点击这个按钮时,它应该转到另一个ViewController(比如说主页)。当我单击按钮时它会改变状态,但重定向不起作用。似乎smth错误的pushViewController?
这是我的代码::
class ViewController: UIViewController, UIPageViewControllerDataSource {
var pageViewController : UIPageViewController!
var pageTitles : NSArray!
var pageImages : NSArray!
let controllers = ["FirstViewController","SecondViewController"]
@IBOutlet weak var restartButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.pageTitles = NSArray(objects: "Explore", "Today Widget")
self.pageImages = NSArray(objects: "page1","page2")
self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController")as! UIPageViewController
self.pageViewController.dataSource = self
var startVC = self.viewControllerAtIndex(0)as ContentViewController
var viewControllers = NSArray(object: startVC)
self.pageViewController.setViewControllers(viewControllers as! [UIViewController], direction: .Forward, animated: true, completion: nil)
self.pageViewController.view.frame = CGRectMake(0,40,self.view.frame.width,self.view.frame.height - 60)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
self.view.backgroundColor = UIColor(red: (25/255.0), green: (127/255.0), blue: (216/255.0), alpha: 1.0)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func restartAction(sender: AnyObject) {
var startVC = self.viewControllerAtIndex(0)as ContentViewController
var viewControllers = NSArray (object: startVC)
self.pageViewController.setViewControllers(viewControllers as! [UIViewController], direction: .Forward, animated: true, completion: nil)
}
func viewControllerAtIndex(index : Int)->ContentViewController
{
if (self.pageTitles.count == 0) || (index >= self.pageTitles.count){
return ContentViewController();
}
var vc:ContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController")as! ContentViewController
vc = self.storyboard?.instantiateViewControllerWithIdentifier(controllers[index])as! ContentViewController
vc.pageIndex = index
return vc;
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
var vc = viewController as! ContentViewController
var index = vc.pageIndex as Int
if (index == 0 || index == NSNotFound){
return nil
}
index--
return self.viewControllerAtIndex(index);
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
var vc = viewController as! ContentViewController
var index = vc.pageIndex as Int
if (index == NSNotFound){
return nil
}
index++
if (index == self.pageTitles.count){
return nil
}
return self.viewControllerAtIndex(index)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return self.pageTitles.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
@IBAction func secondViewController(sender: AnyObject) {
let next = self.storyboard?.instantiateViewControllerWithIdentifier("ThirdViewController") as? ThirdViewController
navigationController?.pushViewController(next!,animated: true)
}