为什么 UIView 不会显示第二次?

Why wont UIView display a second time?

我想弄清楚为什么我的 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] - changing property contentsGravity in transform-only layer, will have no effect 2017-08-10 16:27:59.264702-0700 DogSpotter[960:682273] - changing property contentsGravity in transform-only layer, will have no effect 2017-08-10 16:27:59.374063-0700 DogSpotter[960:682273] libMobileGestalt MobileGestaltSupport.m:153: pid 960 (DogSpotter) does not have sandbox access for frZQaeyWLUvLjeuEK43hmg and IS NOT appropriately entitled 2017-08-10 16:27:59.374151-0700 DogSpotter[960:682273] libMobileGestalt MobileGestalt.c:550: no access to InverseDeviceID (see ) 2017-08-10 16:28:03.321761-0700 DogSpotter[960:682273] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2017-08-10 16:28:03.322541-0700 DogSpotter[960:682273] [MC] Reading from public effective user settings. > You are at 45.4874888481593 -122.734139806728 Dog(name: "Rex", score: 11, picture: size {750, 750} orientation 0 scale 1.000000, location: <+45.48748885,-122.73413981> +/- 65.00m (speed -1.00 mps / course -1.00) @ 8/10/17, 4:28:07 PM Pacific Daylight Time) > 2017-08-10 16:28:44.979563-0700 DogSpotter[960:682273] [App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction 2017-08-10 16:28:44.981668-0700 DogSpotter[960:682273] [App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction You are at 45.4874600607505 -122.734123729821

您必须在 setupNewDogView() 中将 self.newDogView.alpha 设置回 1 并且不要使用 removeFromSuperView,您将丢失对视图的引用。