如何比较 Swift 3 中的 UIViewController? [英] How to compare UIViewController in Swift 3?

查看:36
本文介绍了如何比较 Swift 3 中的 UIViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与 Swift 3 中的 UIViewController 进行比较,但存在一些错误

扩展 UINavigationController{func myPopToViewController(viewController:UIViewController,animated:Bool) ->UIViewController?{var arrViewControllers:[UIViewController] = []arrViewControllers = self.viewControllers对于 arrViewControllers 中的 vc:UIViewController {if(vc.isKind(of: viewController) )//这一行给了我错误{返回(self.navigationController?.popToViewController(vc,动画:动画)?.last)!}}返回零}}

<块引用>

/Users/varunnaharia/Documents/Projects/appname/appname/Public/UINavigationController+Extra.swift:18:30:无法将UIViewController"类型的值转换为预期的参数类型AnyClass"(又名AnyObject.Type"))

如果尝试使用

if(vc 是 viewController)

它给了

<块引用>

/Users/varunnaharia/Documents/Projects/appname/appname/Public/UINavigationController+Extra.swift:18:22:使用未声明的类型viewController"

我通过这个调用它

self.navigationController?.popOrPopToViewController(viewController: MyUIViewController(), 动画: false)

解决方案

如果您想与特定的视图控制器进行比较,您必须比较它们的引用.

试试这个...

if(vc === viewController) ){返回(self.navigationController?.popToViewController(vc,动画:动画)?.last)!}

I am trying to compare to UIViewController in Swift 3 but there is some error

extension UINavigationController
{
    func myPopToViewController(viewController:UIViewController, animated:Bool) -> UIViewController? {
        var arrViewControllers:[UIViewController] = []

        arrViewControllers = self.viewControllers
        for vc:UIViewController in arrViewControllers {
            if(vc.isKind(of: viewController) ) // This Line gives me error
            {
                return (self.navigationController?.popToViewController(vc, animated: animated)?.last)!
            }

        }
        return nil
    }

}

/Users/varunnaharia/Documents/Projects/appname/appname/Public/UINavigationController+Extra.swift:18:30: Cannot convert value of type 'UIViewController' to expected argument type 'AnyClass' (aka 'AnyObject.Type')

and if try to use

if(vc is viewController)

It gives

/Users/varunnaharia/Documents/Projects/appname/appname/Public/UINavigationController+Extra.swift:18:22: Use of undeclared type 'viewController'

I am calling it through this

self.navigationController?.popOrPopToViewController(viewController: MyUIViewController(), animated: false)

解决方案

If you want to compare to a particular view controller you have to compare their refererences.

Try this...

if(vc === viewController) )
{
    return (self.navigationController?.popToViewController(vc, animated: animated)?.last)!
}

这篇关于如何比较 Swift 3 中的 UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆