2016-01-25 15 views
-1

代码:一个错误出来,在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'"

,我应该怎么办???

回答

1

使用下面的代码使用字符串 insted的的NSObject的

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

谢谢!它帮助! – Kaz

+0

@Kaz接受这个答案 –

0

首先,你需要解雇self.dismissViewControllerAnimated(真,完成:无)didFinishPickingMediaWithInfo。

然后,viewWillAppear将在那里被调用,您可以使用push viewcontroller。 由于presentViewController没有使用当前的导航控制器,所以它将无法推动视图控制器