简体   繁体   English

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

[英]How to pass UIViewController as a parameter swift 3?

I'm trying to create a common method for that i have to pass self(current UIViewController ) as a parameter. 我正在尝试创建一个通用方法,我必须传递self(当前UIViewController)作为参数。 Below is the method: 以下是方法:

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

    }

I modified this as follows: 我将其修改如下:

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

But when i pass UIViewController like above it says Cannot convert value of type 'UIViewController.Type' to expected argument type 'UIViewController!' 但是当我像上面那样传递UIViewController时,它说不能将'UIViewController.Type'类型的值转换为预期的参数类型'UIViewController!'

What can i do on this ? 我能做些什么呢?

In swift 3.0, you cannot pass directly your UIViewController. 在swift 3.0中,你无法直接传递你的UIViewController。 First you can create a view controller object. 首先,您可以创建视图控制器对象。

For example, 例如,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 swift如何传递UIViewController类型的参数<XXXDelegate> - swift how to pass parameter of type UIViewController<XXXDelegate> 如何使用 swift 将 UIVIewController 名称作为参数传递给特定函数? - How to pass UIVIewController name as a parameter to particular function using swift? 如何将UIViewController传递给Swift中的另一个类? - How to pass UIViewController to another class in Swift? 如何在 Swift 中的 UIViewController 和 NSObject 之间传递数据 - How to pass data between a UIViewController and an NSObject in Swift 如何在Swift中将数据从UITableViewRowAction传递到UIViewController? - How to pass data to a UIViewController from a UITableViewRowAction in Swift? 如何在Swift中传递Type作为参数 - How to pass a Type as a parameter in Swift 如何将数据传递到 UIView 一侧的嵌入式 UIViewController - Swift 5 - How to pass data to a embedded UIViewController in side a UIView - Swift 5 如何将一个UIViewController类型传递给一个函数并在iOS Swift中输入一个变量? - How to pass a Type of UIViewController to a function and type cast a variable in iOS Swift? Swift-如何将数据从UITableViewCell类传递到UIViewController - Swift - How to Pass Data From UITableViewCell Class to UIViewController 如何从基于Storyboard的应用程序将参数传递给UIViewController? - How to pass parameter to UIViewController from Storyboard based app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM