ユーザーが写真アルバムから画像を選択すると、UIAlertControllerに値を入力するよう求められます。ただし、コントローラが表示されず、画面がクリックされなくなります。UIImagePickerController()から画像を取得した後、UIAlertViewController()を表示しようとしましたが、表示されずに画面がロックされます
ユーザーが写真を撮るときとまったく同じコードを使用するのは奇妙です。
コードは、self.getVal()メソッドが呼び出されていないときに機能します。それは期待どおりに動作します。ロギングでは、コードにエラーはありません。基本的には、現在のviewControllerビューの背後にAlertControllerが作成されているようです。それが解雇されていないので、画面は完全に解除不可能になります。私は、画像を選ぶ前にUIAlertController
に電話をかけることによって、これを解決し
@IBAction func addBarButtonPressed(sender: UIBarButtonItem) {
var alert = UIAlertController(title: "Camera or Existing Photo", message: "Take a photo with the camera, or use an existing photo on your device.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Camera", style: .Default, handler: { (action: UIAlertAction!) in
self.presentCamera()
}))
alert.addAction(UIAlertAction(title: "Photos", style: .Default, handler: { (action: UIAlertAction!) in
self.presentPhotos()
}))
presentViewController(alert, animated: true, completion: nil)
}
func presentPhotos(){
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
imagePickerController.mediaTypes = [kUTTypeImage as String]
imagePickerController.allowsEditing = false
self.presentViewController(imagePickerController, animated: true, completion: nil)
}
}
func presentCamera() {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
imagePickerController.mediaTypes = [kUTTypeImage as String]
imagePickerController.allowsEditing = false
self.presentViewController(imagePickerController, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
self.dismissViewControllerAnimated(true, completion: nil)
if (picker.sourceType == UIImagePickerControllerSourceType.Camera || picker.sourceType == UIImagePickerControllerSourceType.SavedPhotosAlbum)
{
let myImageName = NSUUID().UUIDString
let imagePath = ImageHelperService.fileInDocumentsDirectory(myImageName)
if ImageHelperService.saveImage(image, path: imagePath) {
// TODO:
// Error: This right here, when removed it runs and dismisses and updates fine, just the alertcontroller never appears so can never be dismissed
self.getVal()
while(!self.alertViewDismissed) {
NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate(timeIntervalSinceNow: 0.1))
}
let success = DataOperationsService().SaveThingyMaBob(myImageName, value: self.alertViewValue!, bar: self.bar!)
if success {
self.tableView.reloadData()
self.doChart()
} else {
// TODO: Handle error
debugPrint("Error, save failed")
}
} else {
print("image save failed")
}
}
}
func getVal() {
self.alertViewDismissed = false
let alert = UIAlertController(title: "Enter a value", message: "val", preferredStyle: .Alert)
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.text = ""
textField.keyboardType = .DecimalPad
})
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action) -> Void in
print("Skip")
self.alertViewValue = nil
self.alertViewDismissed = true
}))
alert.addAction(UIAlertAction(title: "Submit", style: .Default , handler: { (action) -> Void in
let textField = alert.textFields![0] as UITextField
print("Text field: \(textField.text)")
self.alertViewValue = UtilitiesService.textFieldUnwrapToDouble(textField.text!)
self.alertViewDismissed = true
}))
self.presentViewController(alert, animated: true, completion: nil)
}
おそらく、self.dismissViewControllerAnimated(true、completion:nil)が問題ですか? dismissViewControllerAnimatedの補完ブロック内にコードを配置しようとしましたか? –
私はそこから 'self.getVal()'を呼び出そうとしましたが、コードブロック全体を試してみます:) –
@JeremyHerrero進歩のビット、AlertControllerが表示されますが、難しくありません。テキストフィールドをクリックしてテキストを入力します。私はさらにデバッグしようとします:) –