代码:
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方法”imagePickerController:didFinishPickingMediaWithInfo:'由方法提供'imagePickerController( didFinishPickingMediaWithInfo :)'与协议'UIImagePickerControllerDelegate'中的可选需求方法'imagePickerController(:didFinishPickingMediaWithInfo :)'冲突
我该怎么做???
答案 0 :(得分:1)
使用下面的代码使用字符串 NSObject
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
//write code here
}
答案 1 :(得分:0)
首先你需要在didFinishPickingMediaWithInfo中解雇self.dismissViewControllerAnimated(true,completion:nil)。
然后将在那里调用viewWillAppear,你可以使用push viewcontroller。 由于presentViewController没有使用当前的导航控制器,因此它无法推送视图控制器