Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将UIViewController作为参数swift 3传递?_Ios_Swift_Swift3_Uiviewcontroller - Fatal编程技术网

Ios 如何将UIViewController作为参数swift 3传递?

Ios 如何将UIViewController作为参数swift 3传递?,ios,swift,swift3,uiviewcontroller,Ios,Swift,Swift3,Uiviewcontroller,我正在尝试创建一个通用方法,我必须将self(当前UIViewController)作为参数传递给它。方法如下: func getFBUserData(){ if let token = FBSDKAccessToken.current(){ print("Access Token: \(token.tokenString!)") FBSDKGraphRequest(graphPath: "me", parameters: ["f

我正在尝试创建一个通用方法,我必须将self(当前UIViewController)作为参数传递给它。方法如下:

  func getFBUserData(){

        if let token = FBSDKAccessToken.current(){

        print("Access Token: \(token.tokenString!)")

            FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
                if (error == nil){
                    self.dict = result as! [String : AnyObject]
                    print(result!)
                    print(self.dict)
                    var jsonObj = JSON(result!)
                    if let first_name = jsonObj["first_name"].string {
                        NSLog("first_name : \(first_name)")
                    }
                    NSLog("RESULT JSON : \(JSON(result!))")
                    //print("Access Token: \(FBSDKAccessToken.current())")
                }
            })
        }

    }
我修改如下:

func IsFaceBookPermisssionGranted(_ completion: @escaping (_ IsPermissionGranted: Bool, _ error_type:ErrorType)->()) {

    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()

    fbLoginManager.logIn(withReadPermissions: ["email"], from: UIViewController?) { (result, error) in
        if (error == nil){
            let fbloginresult : FBSDKLoginManagerLoginResult = result!
            if fbloginresult.grantedPermissions != nil {
                if(fbloginresult.grantedPermissions.contains("email"))
                {
                    completion(true,nil)
                }
            }else{
                completion(false,.AUTH_FAILED)
            }
        }else{
            print("Error : \(error)")
            completion(false,.AUTH_FAILED)
        }
    }
}
但当我通过上面的UIViewController时,它会说 无法将“UIViewController.type”类型的值转换为预期的参数类型“UIViewController!”


对此我能做些什么?

在swift 3.0中, 不能直接传递UIViewController。 首先,可以创建视图控制器对象

比如说,

let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
let yourVC = self.storyboard?.instantiateViewController(withIdentifier: "your_view_controller_Identifier") as! YourViewController

尝试UIViewController.self只需尝试使用self。而不是UIViewController?,传递ViewController对象,在您的情况下,该对象将是
self
。最好创建当前视图控制器的弱引用,然后传递它以避免强引用
weak-var-weakself=self-fbLoginManager.logIn(使用readpermissions:[“email”],from:weakself)
isfacebookpermissiongrated(uu-completion:)创建的此方法在哪里,是否在单独的类中?或者在UIViewController子类中?@NiravD我在viewController中调用它。但是我想在一个普通类中创建这个方法。通过在ViewController中启动该类,我想调用此方法。