2016-11-23 58 views
0

我開始有這個問題,因爲我更新到Xcode 8我不知道它是否可以依賴於一個Xcode錯誤。UIAlertView問題

我有這個問題:this happens when I add this view to a tab bar

它應該是這樣的if you do not tie it to anything it remains unchanged

的問題,是不是我的代碼 我下面添加

import UIKit 

類PhotoSelectViewController:UIViewController中,UINavigationControllerDelegate,UIImagePickerControllerDelegate {

@IBOutlet weak var imageView: UIImageView! 
@IBOutlet weak var selectImageButton: UIButton! 

weak var delegate: PhotoSelectViewControllerDelegate? 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 

@IBAction func selectImageTapped(_ sender: AnyObject) { 
    let imagePicker = UIImagePickerController() 
    imagePicker.delegate = self 

    let actionSheet = UIAlertController(title: "Choose image source", message: nil, preferredStyle: .actionSheet) 

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 

    let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default) { (_) in 
     imagePicker.sourceType = .photoLibrary 
     self.present(imagePicker, animated: true, completion: nil) 
    } 
    let cameraAction = UIAlertAction(title: "Camera", style: .default) { (_) in 
     imagePicker.sourceType = .camera 
     self.present(imagePicker, animated: true, completion: nil) 
    } 

    actionSheet.addAction(cancelAction) 

    if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { 
     actionSheet.addAction(photoLibraryAction) 
    } 
    if UIImagePickerController.isSourceTypeAvailable(.camera) { 
     actionSheet.addAction(cameraAction) 
    } 

    self.present(actionSheet, animated: true, completion: nil) 

    selectImageButton.setTitle("", for: .normal) 

} 

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
     imageView.image = image 
     delegate?.photoSelectViewControllerSelectedImage(image: image) 
    } 
    dismiss(animated: true, completion: nil) 
} 

}

協議PhotoSelectViewControllerDelegate:類{ FUNC photoSelectViewControllerSelectedImage (圖片:用戶界面圖片) }

+0

嘗試給它一個默認的寬度 –

+1

能否請你告訴你如何添加警報? – GoodSp33d

回答

1

UIAlertView中在iOS的8

棄用現在,您需要使用UIAlertController:

let alertController = UIAlertController(title: "Choose image source", message: "", preferredStyle: .actionSheet) 

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { result in 
     print("Destructive") 
    } 

    // Replace UIAlertActionStyle.Default by UIAlertActionStyle.default 
    let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default) { result in 
     print("OK") 
    } 

    alertController.addAction(cancelAction) 
    alertController.addAction(photoLibraryAction) 
    self.present(alertController, animated: true) 

enter image description here

+1

它在iOS 8中已棄用。 – Sahil

+0

已上傳。你可以使用尾隨閉包和類型推斷來稍微好一些。如果你不介意,我更新了你的答案。 – crashoverride777

+0

你的房產也應該以小寫字母開頭 – crashoverride777

1

UIAlertView中已被棄用。使用UIAlertController與UIAlertControllerStyleAlert代替 的preferredStyle在iOS 8,你可以做波紋管

let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert) 
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil)) 
self.present(alert, animated: true, completion: nil)