2016-11-23 56 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)