我试图找出为什么我的newDogView只出现一次。这个应用程序拍摄一张狗照片,将其存储在带有其他信息的结构中,并将地图视图上的狗作为注释绘制。当你按下newDogButton时,会出现一个UIImagePickerController。一旦它被解雇,预览会显示一些添加信息的选项。
用户提交狗照片后,所有内容都会保存到结构中。问题是当用户第二次点击newDogButton时,newDogView永远不会再次显示。
我认为这可能与我调用.removeFromSuperview()的方式有关。
如果有什么突出的,请告诉我。我是新来的!
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, CLLocationManagerDelegate, UITextFieldDelegate {
var image: UIImage?
var location: CLLocation?
var dogs: [Dog] = []
@IBOutlet var newDogButton: UIButton!
@IBOutlet var newDogScore: UILabel!
@IBOutlet var newDogName: UITextField!
@IBOutlet var newDogView: UIView!
@IBOutlet var preview: UIImageView!
@IBOutlet var map: MKMapView!
let locman = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locman.delegate = self
self.locman.requestWhenInUseAuthorization()
self.locman.desiredAccuracy = kCLLocationAccuracyBest
self.map.mapType = .standard
self.map.showsUserLocation = true
self.map.userTrackingMode = .follow
self.newDogName.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
self.navigationController?.setNavigationBarHidden(true, animated: false)
}
@IBAction func newDogTapped(_ sender: Any) {
presentCamera()
self.locman.requestLocation()
}
func presentCamera() {
let source = UIImagePickerControllerSourceType.camera
guard UIImagePickerController.isSourceTypeAvailable(source)
else {
let alert = UIAlertController(title: "Camera Error", message: "Oops! Looks like Dog Spotter doesn't have access to your camera! Please open Settings to give Dog Spotter permission to use the camera.", preferredStyle: .alert)
present(alert, animated: true)
return
}
let camera = UIImagePickerController()
camera.sourceType = source
camera.delegate = self
camera.allowsEditing = true
self.present(camera, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
image = info[UIImagePickerControllerOriginalImage] as? UIImage
if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
image = editedImage
}
self.dismiss(animated: true, completion: {
self.setupNewDogView()
})
}
func setupNewDogView() {
newDogView.isHidden = true
map.isUserInteractionEnabled = false
view.addSubview(newDogView)
newDogView.translatesAutoresizingMaskIntoConstraints = false
newDogView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1, constant: -50).isActive = true
newDogView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 1, constant: -200).isActive = true
newDogView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
newDogView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
preview.image = self.image
newDogView.layer.cornerRadius = 25
newDogButton.layer.cornerRadius = 25
newDogView.isHidden = false
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let loc = locations.last!
let coord = loc.coordinate
location = loc
print("You are at \(coord.latitude) \(coord.longitude)")
}
@IBAction func submitDog(_ sender: Any) {
let newDog = Dog(name: newDogName.text!, score: Int(newDogScore.text!)!, picture: image!, location: location!)
dogs.append(newDog)
print(dogs.last!)
UIView.animate(withDuration: 0.5, animations: {
self.newDogView.alpha = 0
}) { _ in
self.newDogView.removeFromSuperview()
self.map.isUserInteractionEnabled = true
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
return
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
}
Here's an image of my storyboard, if that's helpful!
这是尝试两次显示视图时的控制台输出。
2017-08-10 16:27:59.263965-0700 DogSpotter [960:682273] - 改变属性contentsRravity in transform-only layer,将无效 2017-08-10 16:27:59.264702-0700 DogSpotter [960:682273] - 改变属性contentsRravity in transform-only layer,将无效 2017-08-10 16:27:59.374063-0700 DogSpotter [960:682273] libMobileGestalt MobileGestaltSupport.m:153:piid 960(DogSpotter)对于frZQaeyWLUvLjeuEK43hmg没有沙箱访问权限并且未获得相应的权限 2017-08-10 16:27:59.374151-0700 DogSpotter [960:682273] libMobileGestalt MobileGestalt.c:550:无法访问InverseDeviceID(请参阅参考资料) 2017-08-10 16:28:03.321761-0700 DogSpotter [960:682273] [MC] systemgroup.com.apple.configurationprofiles路径的系统组容器是/private/var/containers/Shared/SystemGroup/systemgroup.com.apple .configurationprofiles 2017-08-10 16:28:03.322541-0700 DogSpotter [960:682273] [MC]从公共有效用户设置中读取。 &GT; 您的位置是45.4874888481593 -122.734139806728 狗(姓名:&#34;雷克斯&#34;,得分:11,图片:尺寸{750,750}方向0比例1.000000,位置:&lt; + 45.48748885,-122.73413981&gt; +/- 65.00m(速度-1.00) mps / course -1.00)@ 8/10 / 17,4:28:07 PM Pacific Daylight Time) &GT; 2017-08-10 16:28:44.979563-0700 DogSpotter [960:682273] [App]如果我们在真正的预提交处理程序中,由于CA限制,我们实际上无法添加任何新的围栏 2017-08-10 16:28:44.981668-0700 DogSpotter [960:682273] [应用]如果我们在真正的预提交处理程序中,由于CA限制,我们实际上无法添加任何新的围栏 你现在的位置是45.4874600607505 -122.734123729821