设置导航控制器的代理?

时间:2016-09-09 03:28:14

标签: ios swift uinavigationcontroller

我按照指南创建自定义转换,他确实创建了一个看起来像的NSObject:

class CustomNavigationAnimationController: NSObject, UINavigationControllerDelegate {

func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

    if fromVC.isKindOfClass(VC2) {
        return showTrans()
    } else if fromVC.isKindOfClass(VC3){
        return dismissTrans()
    } else {
        return nil
    }

}

}

然后在另一个文件中,他创建了两个带有showTrans / dismissTrans函数的类,类看起来像:

  class showTrans: NSObject, UIViewControllerAnimatedTransitioning {  
    func animateTransition(transitionContext:    UIViewControllerContextTransitioning) {
    let containerView = transitionContext.containerView()
    let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
    let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!

    containerView?.addSubview(fromVC.view)
    containerView?.addSubview(toVC.view)

    toVC.view.alpha = 0

    UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [], animations: { 
        toVC.view.alpha = 1
    }) { (finished: Bool) in
            transitionContext.completeTransition(true)
    }

}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
    return 0.7
}

在视频的最开始,他进入故事板并点击他的导航控制器,顶部有4个按钮,用于导航控制器/第一响应者/退出/代表。他单击委托按钮并将Nav Bar的委托设置为CustomNavigationAnimationController类。当我点击导航控制器时,我只会将导航控制器/第一响应者/退出按钮放在顶部。

如何将类/转换代码连接到导航控制器?

2 个答案:

答案 0 :(得分:1)

在故事板中单击Navigation Controller。将NSObject从对象库添加到导航栏。在视图的顶部,您可以看到四个按钮(如您在问题中所述 - 导航控制器/第一响应者/ NSObject /退出按钮)。右键单击第一个按钮。您将获得导航控制器动作列表。从 Outlets 下面选择委托并拖动到NSObject。将NSObject连接到您的班级。

答案 1 :(得分:1)

您可以在故事板中的导航控制器中看到箭头标记。单击该箭头标记,您将看到导航栏,然后右键单击它以获取代理。

以下是让您轻松的快照

enter image description here