首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在swift 4中未弹出用于选择相机或图库的警报视图

在swift 4中未弹出用于选择相机或图库的警报视图
EN

Stack Overflow用户
提问于 2018-08-27 03:43:00
回答 1查看 1.1K关注 0票数 1

我有一个带有导航控制器的视图控制器。我有上传images.But的按钮,按下按钮后,它不会弹出相机或图片库选项的警报,尽管我已经编写了警报代码。为什么没有出现警报弹出窗口?我还在info.plist中给出了隐私-图片库的使用说明。我的代码如下:

import UIKit
import  Alamofire

class ProfilePicController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


var imagePicker = UIImagePickerController()


@IBOutlet weak var uploadPic: UIButton!
@IBOutlet weak var profilePic: UIImageView!


override func viewDidLoad() {
    super.viewDidLoad()

  imagePicker.delegate = self
  profilePic.layer.cornerRadius = profilePic.frame.size.width / 2
  profilePic.clipsToBounds = true


    // Do any additional setup after loading the view.
}


@IBAction func uploadPic(_ sender: Any) {

 print("image add")

let alert = UIAlertController(title: nil, message: "Choose your source", preferredStyle: UIAlertControllerStyle.alert)

alert.addAction(UIAlertAction(title: "Camera", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
  print("Camera selected")
  self.imagePicker.sourceType = UIImagePickerControllerSourceType.camera

})
alert.addAction(UIAlertAction(title: "Photo library", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
  print("Photo selected")
  self.imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary

})

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

}
 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
 let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

 profilePic.image = selectedImage
 // self.savePic()
 dismiss(animated: true, completion: nil)
 }

 func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
 dismiss(animated: true, completion: nil)
}

//Alert.swift文件

import UIKit

class Alert {

class func showBasic(title: String ,message: String , vc: UIViewController) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(defaultAction)
vc.present(alert, animated: true, completion: nil)
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-27 03:45:55

实际上,您并没有显示警报

@IBAction func uploadPic(_ sender: Any) {

  print("image add")

  let alert = UIAlertController(title: nil, message: "Choose your source", preferredStyle: UIAlertControllerStyle.alert)

  alert.addAction(UIAlertAction(title: "Camera", style: .default) { (result : UIAlertAction) -> Void in
    print("Camera selected")
    self.imagePicker.sourceType = .camera
    self.present(self.imagePicker, animated: true, completion: nil)
  })
  alert.addAction(UIAlertAction(title: "Photo library", style: .default) { (result : UIAlertAction) -> Void in
    print("Photo selected")
    self.imagePicker.sourceType = .photoLibrary
    self.present(self.imagePicker, animated: true, completion: nil)
  })

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

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52029705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档