func imagePickerController 出现错误

An Error comes out in func imagePickerController

代码:

AppDelegate.swift

import UIKit

@UIApplicationMain
 class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var myViewController: UIViewController?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {

    //ViewControllerのインスタンス化
    myViewController = ViewController()

    //UINavigationControllerのインスタンス化とrootViewControllerの指定
    var myNavigationController = UINavigationController(rootViewController: myViewController!)

    //UIWindowのインスタンス化
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    //UIWindowのrootViewControllerにnavigationControllerを指定
    self.window?.rootViewController = myNavigationController

    //UIWindowの表示
    self.window?.makeKeyAndVisible()

    return true

}

}

ViewController.swift

import UIKit

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

var myImagePicker: UIImagePickerController!
var myImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "Select a Image"

    myImageView = UIImageView(frame: self.view.bounds)

    // インスタンス生成
    myImagePicker = UIImagePickerController()

    // デリゲート設定
    myImagePicker.delegate = self

    // 画像の取得先はフォトライブラリ
    myImagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

    // 画像取得後の編集を不可に
    myImagePicker.allowsEditing = false
}

override func viewDidAppear(animated: Bool) {
    self.presentViewController(myImagePicker, animated: true, completion: nil)

}

/**
 画像が選択された時に呼ばれる.
 */
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

    //選択された画像を取得.
    var myImage: AnyObject?  = info[UIImagePickerControllerOriginalImage]

    //選択された画像を表示するViewControllerを生成.
    let secondViewController = SecondViewController()

    //選択された画像を表示するViewContorllerにセットする.
    secondViewController.mySelectedImage = myImage as! UIImage

    myImagePicker.pushViewController(secondViewController, animated: true)

}

/**
 画像選択がキャンセルされた時に呼ばれる.
 */
func imagePickerControllerDidCancel(picker: UIImagePickerController) {

    // モーダルビューを閉じる
    self.dismissViewControllerAnimated(true, completion: nil)
}

}

View2.swift

import Foundation
import UIKit

class SecondViewController: UIViewController {

var mySelectedImage: UIImage!
var mySelectedImageView: UIImageView!

override func viewDidLoad() {

    self.edgesForExtendedLayout = UIRectEdge.None
    self.view.backgroundColor = UIColor.whiteColor()

    setImage()
}

/**
 選択された画像をUIImageViewにセットする.
 */
func setImage(){
    self.title = "Selected Image"

    mySelectedImageView = UIImageView(frame: self.view.bounds)
    mySelectedImageView.contentMode = UIViewContentMode.ScaleAspectFit
    mySelectedImageView.image = mySelectedImage
    self.view.addSubview(mySelectedImageView)
}

}

ViewController.swift 中,我得到以下错误:

"Objective-C method 'imagePickerController:didFinishPickingMediaWithInfo:'provided by method 'imagePickerController(didFinishPickingMediaWithInfo:)'conflicts with optional requirement method 'imagePickerController(:didFinishPickingMediaWithInfo:)'in protocol 'UIImagePickerControllerDelegate'"

我该怎么办???

使用下面的代码使用 String insted of NSObject

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
//write code here
 }

首先你需要在 didFinishPickingMediaWithInfo 中关闭 self.dismissViewControllerAnimated(true, completion: nil)。

然后 viewWillAppear 会被调用,你可以使用 push viewcontroller 。 由于 presentViewController 没有使用当前的导航控制器,因此它将无法推送 viewcontroller