2016-11-30 21 views
-1

我想要用戶在拍攝照片後點擊「使用照片」後進入新的viewController 。但是,賽格從未被執行過。我已經嘗試通過手動連接兩個viewControllers來實現這一功能。我的imagePickerController函數中有一個print語句,這是我的segue代碼的地方。當我點擊「使用照片」時會打印此打印聲明,爲什麼我的細節被忽略?我在「@IBAction func someButton(_sender:UIButton)」中嘗試了完全相同的segue代碼,它工作正常嗎?任何幫助,將不勝感激。當用照相機拍照後點擊「使用照片」按鈕時,執行segue到一個新的viewController iOS 10(swift 3)

這裏是我的代碼:

import UIKit 

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate { 

@IBOutlet weak var imageTake: UIImageView! 

var imagePicker: UIImagePickerController! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 


override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 





@IBAction func takePhoto(_ sender: UIButton) { 
    imagePicker = UIImagePickerController() 
    imagePicker.delegate = self 
    imagePicker.sourceType = .camera 
    present(imagePicker, animated: true, completion: nil) 

} 

@IBAction func photoLib(_ sender: UIButton) { 
    imagePicker = UIImagePickerController() 
    imagePicker.delegate = self 
    imagePicker.sourceType = .photoLibrary 
    present(imagePicker, animated: true, completion: nil) 

} 

@IBAction func save(_ sender: AnyObject) { 
    UIImageWriteToSavedPhotosAlbum(imageTake.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil) 


} 

func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) { 
    if let error = error { 
     // we got back an error! 
     let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .default)) 
     present(ac, animated: true) 
    } else { 
     let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .default)) 
     present(ac, animated: true) 
    } 
} 




func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    imagePicker.dismiss(animated: true, completion: nil) 
    imageTake.image = info[UIImagePickerControllerOriginalImage] as? UIImage 
    print("made it") 
    performSegue(withIdentifier: "new", sender: self) 



} 

}

+0

也許''新「'不是你的segue的標識符。 – matt

+0

這是因爲當我把相同的代碼放在像「@IBAction func someButton(_sender:UIButton)」這樣的任意按鈕中時,它的工作原理 –

+0

是否將它「打印出來」? – matt

回答

0

這裏有一個建議。你有這樣的:

imagePicker.dismiss(animated: true, completion: nil) 
// ... 
performSegue(withIdentifier: "new", sender: self) 

它改成這樣:

imagePicker.dismiss(animated: true, completion: { 
    performSegue(withIdentifier: "new", sender: self) 
}) 

這樣的話,我們不嘗試,直到圖像選擇器實際上是完全駁回執行SEGUE。

如果不工作,那麼我將不得不得出結論,有沒有這樣的賽格瑞爲"new"從這個視圖控制器來了。但是你沒有展示你的故事板,所以我不能確定。

+0

這是修復! @馬特 –

相關問題