Xcode Swift:无法关闭弹出图片

时间:2015-08-15 23:49:08

标签: ios xcode swift uiscrollview uiimageview

我创建了一个ViewController,通过三个单独的UIImageViews通过JSON解析显示三个图像和其他信息。当您点击任意图片时,会转到另一个ViewController,在后台弹出UIScrollView,其中一个UIImageView与所有三个图片相关联,{{1}这将关闭弹出窗口Button并将其恢复到上一个​​弹出窗口。这是一个screenshot。我遇到的问题是我添加了这段代码:

ViewController

当我尝试点击关闭按钮时,没有任何反应。不知道我错过了什么。任何指导都会有所帮助。

在这里,我将为两个控制器设置代码:

JnsDetail.swift

func removeZoom()
    {
        UIView.animateWithDuration(0.25, animations: {
            self.view.transform = CGAffineTransformMakeScale(1.3, 1.3)
            self.view.alpha = 0.0;
            }, completion:{(finished : Bool)  in
                if (finished)
                {
                    self.view.removeFromSuperview()
                }
        });
    }

    @IBAction func closeZoom(sender: AnyObject) {

        self.navigationController?.popToRootViewControllerAnimated(true)

    }

JnsZoomController.swift

import Foundation
import UIKit

class JnsDetail: UIViewController {

    @IBOutlet var tituloLabel : UILabel!
    @IBOutlet var marcaLabel : UILabel!
    @IBOutlet var colorLabel : UILabel!
    @IBOutlet var tipoLabel : UILabel!
    @IBOutlet var refLabel : UILabel!

    @IBOutlet var imageView : UIImageView!
    @IBOutlet var imageView2 : UIImageView!
    @IBOutlet var imageView3 : UIImageView!

    @IBOutlet var backbutton : UIButton!

    var jsonextrct : JsonExtrct!

    var photos : [String]!

    var transitionOperator = TransitionOperator()

    override func viewDidLoad() {
        super.viewDidLoad()

        //titulo = jsonextrct.titulo

        tituloLabel.font = UIFont(name: mTheme.fontName, size: 21)
        tituloLabel.textColor = UIColor.blackColor()
        tituloLabel.text = jsonextrct.titulo

        //marca = jsonextrct.marca

        marcaLabel.font = UIFont(name: mTheme.fontName, size: 21)
        marcaLabel.textColor = UIColor.blackColor()
        marcaLabel.text = jsonextrct.marca

        //color = jsonextrct.color

        colorLabel.font = UIFont(name: mTheme.fontName, size: 21)
        colorLabel.textColor = UIColor.blackColor()
        colorLabel.text = jsonextrct.color

        //tipo = jsonextrct.tipo

        tipoLabel.font = UIFont(name: mTheme.fontName, size: 21)
        tipoLabel.textColor = UIColor.blackColor()
        tipoLabel.text = jsonextrct.tipo

        //ref = jsonextrct.ref

        refLabel.font = UIFont(name: mTheme.fontName, size: 21)
        refLabel.textColor = UIColor.blackColor()
        refLabel.text = "\(jsonextrct.ref)"

        if let imageData = jsonextrct.imageData {
            imageView.image = UIImage(data: imageData)
        }else{
            Utils.asyncLoadJsonImage(jsonextrct, imageView: imageView)
        }
        //topImageViewHeightConstraint.constant = 240

        imageView.layer.borderColor = UIColor(white: 0.2, alpha: 1.0).CGColor
        imageView.layer.borderWidth = 0.5

        if let imageData2 = jsonextrct.imageData2 {
            imageView2.image = UIImage(data: imageData2)
        }else{
            Utils.asyncLoadJsonImage(jsonextrct, imageView2: imageView2)
        }

        imageView2.layer.borderColor = UIColor(white: 0.2, alpha: 1.0).CGColor
        imageView2.layer.borderWidth = 0.5

        if let imageData3 = jsonextrct.imageData3 {
            imageView3.image = UIImage(data: imageData3)
        }else{
            Utils.asyncLoadJsonImage(jsonextrct, imageView3: imageView3)
        }

        imageView3.layer.borderColor = UIColor(white: 0.2, alpha: 1.0).CGColor
        imageView3.layer.borderWidth = 0.5

        var tapGestureZoom = UITapGestureRecognizer(target: self, action: "zoomJns:")
        tapGestureZoom.numberOfTapsRequired = 1
        tapGestureZoom.numberOfTouchesRequired = 1
        imageView.userInteractionEnabled = true
        imageView.addGestureRecognizer(tapGestureZoom)

        var tapGestureZoom2 = UITapGestureRecognizer(target: self, action: "zoomJns2:")
        tapGestureZoom2.numberOfTapsRequired = 1
        tapGestureZoom2.numberOfTouchesRequired = 1
        imageView2.userInteractionEnabled = true
        imageView2.addGestureRecognizer(tapGestureZoom2)

        var tapGestureZoom3 = UITapGestureRecognizer(target: self, action: "zoomJns3:")
        tapGestureZoom3.numberOfTapsRequired = 1
        tapGestureZoom3.numberOfTouchesRequired = 1
        imageView3.userInteractionEnabled = true
        imageView3.addGestureRecognizer(tapGestureZoom3)

    }

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return UIStatusBarStyle.Default
    }

    func backTapped(sender: AnyObject?){
        dismissViewControllerAnimated(true, completion: nil)
    }

    @IBAction func zoomJns(sender: AnyObject?){
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("JnsZoomController") as! JnsZoomController
        self.modalPresentationStyle = UIModalPresentationStyle.Custom
        controller.transitioningDelegate = transitionOperator
        controller.jsonextrct = jsonextrct

        presentViewController(controller, animated: true, completion: nil)
    }

    @IBAction func zoomJns2(sender: AnyObject?){
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("JnsZoomController") as! JnsZoomController
        self.modalPresentationStyle = UIModalPresentationStyle.Custom
        controller.transitioningDelegate = transitionOperator
        controller.jsonextrct = jsonextrct

        presentViewController(controller, animated: true, completion: nil)
    }

    @IBAction func zoomJns3(sender: AnyObject?){
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("JnsZoomController") as! JnsZoomController
        self.modalPresentationStyle = UIModalPresentationStyle.Custom
        controller.transitioningDelegate = transitionOperator
        controller.jsonextrct = jsonextrct

        presentViewController(controller, animated: true, completion: nil)
    }

}

2 个答案:

答案 0 :(得分:1)

这就是我所看到的问题,如果你是"弹出"根视图控制器这意味着你必须将视图控制器推到导航控制器的堆栈上,我不会看到你将任何东西推到导航堆栈上。除非出于某种原因苹果决定杀掉Pushing视图控制器,但我怀疑是这种情况。因此,我在您的代码中看到的另一个问题是。您只是通过呈现视图控制器来呈现视图控制器,如果您调用

,我不会通过使用导航控制器来呈现视图控制器SOOO来查看您在哪里呈现视图控制器
     self.navigationController?.popToRootViewControllerAnimated(true)
然后,由于您在另一个视图控制器上以模态方式呈现了视图控制器而没有在视图控制器的导航控制器中显示模态,因此堆栈中没有任何内容导航控制器要从堆栈中移除。

解决方案,也许,但这不是100%因为我没有在你面前提供你的代码。

改变这个:

     self.navigationController?.popToRootViewControllerAnimated(true)

这样的事情

self.dismissViewControllerAnimated(animated: true, completion:nil)

我不做快速,所以我的解决方案是伪代码,随意添加问号以及Apple认为不具备某些原因的价值。

您也可以将演示文稿更改为:

self.navigationController?.presentViewController(controller, animated: true, completion: nil)

同样,以上是伪代码但我认为我将问号放在正确的位置,以便它做它想做的事情

此外,您可以参考这一点,虽然Apple并没有真正告诉您高级导航堆栈如何工作:

  

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/pushViewController:animated

有时您需要同时运行4-10个导航控制器,因此请确保您了解它们与视图控制器的交互方式,并确保您了解POP,PUSH和PRESENT的作用。祝你好运,祝你有个美好的一天。

答案 1 :(得分:1)

在closeZoom中,我认为你应该只使用

@IBAction func closeZoom(sender: AnyObject) {

    dismissViewControllerAnimated(true, completion: nil)

}

因为您提供了View Controller,所以在推送时使用了popToRootViewControllerAnimated(true)