如何以编程方式滚动到滚动视图容器中的下一个视图控制器

时间:2014-11-09 15:51:46

标签: swift delegates protocols snapchat

我创建了一个包含三个视图控制器的滚动视图容器。它的意思是模仿Snapchat的滑动布局。但是,我似乎无法获得一个代码来手动切换到下一个视图控制器而不实际刷卡(我不感兴趣)

我尝试调用容器类并设置它的滚动偏移但它崩溃了...尝试创建委托协议,但委托正在返回nil ...我很难过。自凌晨1点以来一直在上升。

这是我的代码......

class AViewController: UIViewController, ABViewControllerDelegate {

@IBOutlet var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()
    // 1) Create the three views used in the swipe container view
    var ATVc : ATViewController =  ATViewController(nibName: "ATViewController", bundle: nil);
    var ACVc : ACViewController =  ACViewController(nibName: "ACViewController", bundle: nil);
    var ABVc : ABViewController =  ABViewController(nibName: "ABViewController", bundle: nil);

    // 2) Add in each view to the container view hierarchy
    //    Add them in opposite order since the view hieracrhy is a stack
    self.addChildViewController(ABVc);
    self.scrollView!.addSubview(ABVc.view);
    ABVc.didMoveToParentViewController(self);

    self.addChildViewController(ACVc);
    self.scrollView!.addSubview(ACVc.view);
    ACVc.didMoveToParentViewController(self);

    self.addChildViewController(ATVc);
    self.scrollView!.addSubview(ATVc.view);
    ATVc.didMoveToParentViewController(self)

    // 3) Set up the frames of the view controllers to align
    //    with eachother inside the container view
    var adminFrame :CGRect = ATVc.view.frame;
    adminFrame.origin.y = adminFrame.height;
    ACVc.view.frame = adminFrame;

    var BFrame :CGRect = ACVc.view.frame;
    BFrame.origin.y = 2*BFrame.height;
    ABVc.view.frame = BFrame;

    // 4) Finally set the size of the scroll view that contains the frames
    var scrollWidth: CGFloat  = self.view.frame.width
    var scrollHeight: CGFloat  = 3 * self.view.frame.size.height
    self.scrollView!.contentSize = CGSizeMake(scrollWidth, scrollHeight)
    self.scrollView!.setContentOffset(CGPointMake(0, self.view.frame.height), animated: false)

    var changeMe : String = "okay"
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func scrollUp() {
    println("clicked!")
    self.scrollView.contentOffset.y - self.view.frame.height
}

}

这是视图控制器,我试图通过按下按钮来取消...

protocol ABViewControllerDelegate {
func scrollUp()
}

class ABViewController: UIViewController {

let delegate = ABViewControllerDelegate?()

@IBAction func button(sender: AnyObject) {
    println("button clicked!")
    delegate!.scrollUp()
}
override func viewDidLoad() {
    super.viewDidLoad()
    // 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.
}

}

我一直在寻找几个小时的答案,我觉得自己已经领先于自己并且无法完成!任何帮助将非常感激。谢谢你们:))

0 个答案:

没有答案