0

我创建了一个包含三个视图控制器的滚动视图容器。它旨在模仿 snapchat 的滑动布局。但是,我似乎无法在没有实际滑动的情况下手动切换到下一个视图控制器的代码(我对此不感兴趣)

我尝试调用容器类并将其设置为滚动偏移,但它崩溃了……尝试创建委托协议,但委托返回 nil……我很难过。

这是我的代码:

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"
    }

    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()
    }
}

我觉得我在领导自己,这是不可能的!

4

0 回答 0