2016-01-20 34 views
1

內部FUNC我有一個配置文件類和設置類使用從一個類到另一個類中迅速

型材類包含一個內部功能

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) 

    } 
} 

上述碼結果到Cannot convert value of type 'SettingsVC' to expected argument type 'Profile'因爲profileSelectFromGallery需要一個類的參數so 我想要做的是更改發件人,以便我可以使用它從我的任何課程,而不僅僅是我的Profile類

回答

5

所以問題是,你不能轉換SettingsVCProfile。如果你看一下方法簽名,你會看到它的期待Profile

internal func profileSelectFromGallery(sender: Profile)

您試圖在SettingVC通過在selectFromGallery()

裏面profileSelectFromGallery你想發送者既是UIViewControllerUIImagePickerControllerDelegate。有幾種方法可以做到這一點:

最簡單的方法是更改​​方法簽名。你會做這樣的事情:

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

即使世界2主這裏的東西:sender更改爲正確的委託方法,並且那裏有一個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多數民衆贊成也是一個UIViewControllerUINAvigationControllerDelegate。這意味着我可以在Profile和SettingVC上調用它(一旦您將必要的代理添加到SettingVC)。所有你需要做的是:

let profile = Profile() 
profile.profileSelectFromGallery() 

let settingVC = SettingVC() 
settingVC.profileSelectFromGallery() 
0

即使它是一種實例方法,您仍試圖靜態調用profileSelectFromGallery:

試着改變方法定義:

internal static func profileSelectFromGallery(sender: Profile){ 

至於能夠使用任何類作爲一個代表,創建一個自定義Protocol並確保sender符合該協議。看到這裏(具體題目Protocols標題)的詳細信息:http://www.raywenderlich.com/115300/swift-2-tutorial-part-3-tuples-protocols-delegates-and-table-views

+0

我的問題是我怎麼能將發件人更改爲類似於任何類的東西,以便我可以將它用作deletegat myPickerController.delegate = sender;' –

+0

用鏈接t應該讓你開始。 –

3

聲明新的協議爲:

protocol PickerProtocol : UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

}

現在您的個人資料類看起來像:

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 SettingsVC1: UITableViewController { 
    // I will call this private function on a click events 
    private func selectFromGallery(){ 

     let profile = Profile() 
     profile.profileSelectFromGallery(self, pickerProtocol:profile) 
     profile.profileSelectFromGalleryOption2(self) 
     //Or 
     profile.profileSelectFromGalleryOption2(nil)//profile itself delegate and presenting controller 
    } 
} 

// OR

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

     let profile = Profile() 
     profile.profileSelectFromGallery(self, pickerProtocol:self) 
     profile.profileSelectFromGalleryOption2(self) 
     //Or 
     profile.profileSelectFromGalleryOption2(nil)//profile itself delegate and presenting controller 
    } 
} 
0

也許下面的工作:

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

我會用POP(協議面向對象編程)這樣的:

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() 
    } 
} 
相關問題