Ios 在swift中使用从一个类到另一个类的内部func

Ios 在swift中使用从一个类到另一个类的内部func,ios,swift,swift2,Ios,Swift,Swift2,我有一个配置文件类和设置类 profile类包含一个内部函数 class Profile: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate { internal func profileSelectFromGallery(sender: Profile){ let myPickerController = UIImagePickerController()

我有一个配置文件类和设置类

profile类包含一个内部函数

class Profile: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

   internal func profileSelectFromGallery(sender: Profile){
        let myPickerController = UIImagePickerController()
        myPickerController.delegate = sender;
        myPickerController.sourceType =
            UIImagePickerControllerSourceType.PhotoLibrary
        sender.presentViewController(myPickerController, animated:true, completion: nil)
    }
}
我想在设置类中使用profileSelectFromGallery,下面我有两次尝试

class SettingsVC: UITableViewController {
   // I will call this private function on a click events
   private func selectFromGallery(){

            // let profile = Profile()
            // profile.profileSelectFromGallery(self)
            Profile.profileSelectFromGallery(self)

   }
}

由于profileSelectFromGallery需要类配置文件的参数,因此上述代码无法将“SettingsVC”类型的值转换为预期的参数类型“Profile”,因此我要做的是更改sender,以便我可以从我的任何类而不仅仅是我的配置文件类中使用它。

您正在尝试静态调用profileSelectFromGallery:即使它是一个实例方法

尝试将方法定义更改为:

internal static func profileSelectFromGallery(sender: Profile){

至于能够使用任何类作为委托,请创建一个自定义协议,并确保发送方遵守此协议。详细信息请参见标题为Protocols的标题:

您正在尝试静态调用profileSelectFromGallery:即使它是一个实例方法

尝试将方法定义更改为:

internal static func profileSelectFromGallery(sender: Profile){

至于能够使用任何类作为委托,请创建一个自定义协议,并确保发送方遵守此协议。请参阅此处标题为Protocols的标题以了解更多信息:

因此,问题在于无法将SettingsVC转换为配置文件。如果查看方法签名,您将看到它需要一个配置文件:

内部功能配置文件SelectFromGallerySsender:配置文件

您正试图在selectFromGallery中传入设置VC

在Profile中选择FromGallery,您希望发送者同时是UIViewController和UIImagePickerController Delegate。有几种方法可以做到这一点:

最简单的方法是更改方法签名。你可以这样做:

   internal func profileSelectFromGallery(sender: UIImagePickerControllerDelegate){
        guard let vc = sender as? UIViewController else {
           return
        }

        let myPickerController = UIImagePickerController()
        myPickerController.delegate = sender;
        myPickerController.sourceType =
            UIImagePickerControllerSourceType.PhotoLibrary
        vc.presentViewController(myPickerController, animated:true, completion: nil)
    }
protocol SelectorProtocol: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
}

extension SelectorProtocol where Self: UIViewController {
    func profileSelectFromGallery() {
        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self;
        myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        self.presentViewController(myPickerController, animated:true, completion: nil)
    }
}

class Profile: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate, SelectorProtocol {
    func foo() {
        profileSelectFromGallery()
    }
}

class SettingsVC: UITableViewController, SelectorProtocol {
    // I will call this private function on a click events
    private func selectFromGallery(){
        profileSelectFromGallery()
    }
}
这里有两件主要的事情:发送方被更改为正确的委托方法,并且有一个guard语句将其转换为VC以进行presentViewController调用

更可怕的方法是使用协议扩展

extension UIImagePickerControllerDelegate where Self: UIViewController, Self: UINavigationControllerDelegate {
    func profileSelectFromGallery() {
        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self
        myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        self.presentViewController(myPickerController, animated:true, completion: nil)
    }
}
基本上,我在这里做的是为每个UIImagePickerControllerDelegate添加一个方法,它也是一个UIViewController和一个UINAvigationControllerDelegate。这意味着,一旦向SettingVC添加了必要的委托,我就可以在Profile和SettingVC上调用它。您需要做的只是:

let profile = Profile()
profile.profileSelectFromGallery()

let settingVC = SettingVC()
settingVC.profileSelectFromGallery()

因此,问题是您无法将SettingsVC转换为配置文件。如果查看方法签名,您将看到它需要一个配置文件:

内部功能配置文件SelectFromGallerySsender:配置文件

您正试图在selectFromGallery中传入设置VC

在Profile中选择FromGallery,您希望发送者同时是UIViewController和UIImagePickerController Delegate。有几种方法可以做到这一点:

最简单的方法是更改方法签名。你可以这样做:

   internal func profileSelectFromGallery(sender: UIImagePickerControllerDelegate){
        guard let vc = sender as? UIViewController else {
           return
        }

        let myPickerController = UIImagePickerController()
        myPickerController.delegate = sender;
        myPickerController.sourceType =
            UIImagePickerControllerSourceType.PhotoLibrary
        vc.presentViewController(myPickerController, animated:true, completion: nil)
    }
protocol SelectorProtocol: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
}

extension SelectorProtocol where Self: UIViewController {
    func profileSelectFromGallery() {
        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self;
        myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        self.presentViewController(myPickerController, animated:true, completion: nil)
    }
}

class Profile: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate, SelectorProtocol {
    func foo() {
        profileSelectFromGallery()
    }
}

class SettingsVC: UITableViewController, SelectorProtocol {
    // I will call this private function on a click events
    private func selectFromGallery(){
        profileSelectFromGallery()
    }
}
这里有两件主要的事情:发送方被更改为正确的委托方法,并且有一个guard语句将其转换为VC以进行presentViewController调用

更可怕的方法是使用协议扩展

extension UIImagePickerControllerDelegate where Self: UIViewController, Self: UINavigationControllerDelegate {
    func profileSelectFromGallery() {
        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self
        myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        self.presentViewController(myPickerController, animated:true, completion: nil)
    }
}
基本上,我在这里做的是为每个UIImagePickerControllerDelegate添加一个方法,它也是一个UIViewController和一个UINAvigationControllerDelegate。这意味着,一旦向SettingVC添加了必要的委托,我就可以在Profile和SettingVC上调用它。您需要做的只是:

let profile = Profile()
profile.profileSelectFromGallery()

let settingVC = SettingVC()
settingVC.profileSelectFromGallery()

宣布新协议为:

protocol PickerProtocol : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
}

现在,您的Profile类将如下所示:

class Profile: UIViewController, PickerProtocol {

//Option 1
internal func profileSelectFromGallery(contoller: UIViewController, pickerProtocol: PickerProtocol){

    let myPickerController = UIImagePickerController()

    myPickerController.delegate = pickerProtocol

    myPickerController.sourceType =
        UIImagePickerControllerSourceType.PhotoLibrary
    contoller.presentViewController(myPickerController, animated:true, completion: nil)
}

//Option 2
internal func profileSelectFromGalleryOption2(sender : UIViewController? ) {

    var viewContoller : UIViewController = self
    if let unwrappedSender = sender {
        viewContoller = unwrappedSender
    }

    let myPickerController = UIImagePickerController()

    if let pickerProtocol = viewContoller as? PickerProtocol {
        myPickerController.delegate = pickerProtocol
    } else {
        myPickerController.delegate = self //Assign self as default
    }

    myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    viewContoller.presentViewController(myPickerController, animated:true, completion: nil)
}
}

//或


宣布新协议为:

protocol PickerProtocol : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
}

现在,您的Profile类将如下所示:

class Profile: UIViewController, PickerProtocol {

//Option 1
internal func profileSelectFromGallery(contoller: UIViewController, pickerProtocol: PickerProtocol){

    let myPickerController = UIImagePickerController()

    myPickerController.delegate = pickerProtocol

    myPickerController.sourceType =
        UIImagePickerControllerSourceType.PhotoLibrary
    contoller.presentViewController(myPickerController, animated:true, completion: nil)
}

//Option 2
internal func profileSelectFromGalleryOption2(sender : UIViewController? ) {

    var viewContoller : UIViewController = self
    if let unwrappedSender = sender {
        viewContoller = unwrappedSender
    }

    let myPickerController = UIImagePickerController()

    if let pickerProtocol = viewContoller as? PickerProtocol {
        myPickerController.delegate = pickerProtocol
    } else {
        myPickerController.delegate = self //Assign self as default
    }

    myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    viewContoller.presentViewController(myPickerController, animated:true, completion: nil)
}
}

//或


也许以下措施会奏效:

   class SettingsVC: UITableViewController {
   // I will call this private function on a click events
   private func selectFromGallery(){
       let prof = Profile()
       prof.profileSelectFromGallery(prof)
   }
}

也许以下措施会奏效:

   class SettingsVC: UITableViewController {
   // I will call this private function on a click events
   private func selectFromGallery(){
       let prof = Profile()
       prof.profileSelectFromGallery(prof)
   }
}

我将使用以下面向POP协议的编程:

   internal func profileSelectFromGallery(sender: UIImagePickerControllerDelegate){
        guard let vc = sender as? UIViewController else {
           return
        }

        let myPickerController = UIImagePickerController()
        myPickerController.delegate = sender;
        myPickerController.sourceType =
            UIImagePickerControllerSourceType.PhotoLibrary
        vc.presentViewController(myPickerController, animated:true, completion: nil)
    }
protocol SelectorProtocol: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
}

extension SelectorProtocol where Self: UIViewController {
    func profileSelectFromGallery() {
        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self;
        myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        self.presentViewController(myPickerController, animated:true, completion: nil)
    }
}

class Profile: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate, SelectorProtocol {
    func foo() {
        profileSelectFromGallery()
    }
}

class SettingsVC: UITableViewController, SelectorProtocol {
    // I will call this private function on a click events
    private func selectFromGallery(){
        profileSelectFromGallery()
    }
}

我将使用以下面向POP协议的编程:

   internal func profileSelectFromGallery(sender: UIImagePickerControllerDelegate){
        guard let vc = sender as? UIViewController else {
           return
        }

        let myPickerController = UIImagePickerController()
        myPickerController.delegate = sender;
        myPickerController.sourceType =
            UIImagePickerControllerSourceType.PhotoLibrary
        vc.presentViewController(myPickerController, animated:true, completion: nil)
    }
protocol SelectorProtocol: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
}

extension SelectorProtocol where Self: UIViewController {
    func profileSelectFromGallery() {
        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self;
        myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        self.presentViewController(myPickerController, animated:true, completion: nil)
    }
}

class Profile: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate, SelectorProtocol {
    func foo() {
        profileSelectFromGallery()
    }
}

class SettingsVC: UITableViewController, SelectorProtocol {
    // I will call this private function on a click events
    private func selectFromGallery(){
        profileSelectFromGallery()
    }
}

我的问题是如何将发送方更改为类似于anyclass的内容,以便将其用作deletegat myPickerController.delegate=sender;用一个链接更新了我的答案,应该可以让您开始。我的问题是如何将发件人更改为类似anyclass的内容,以便将其用作deletegat myPickerController.delegate=sender;用一个链接更新了我的答案,应该可以让你开始。