2015-08-15 72 views
0

我創建了一個ViewController,它通過JSON解析通過三個單獨的UIImageViews顯示三個圖像和其他信息。當您點擊任何圖像時,會將您帶到另一個ViewController,在後臺彈出UIScrollView,一個UIImageView鏈接到所有三個圖像和一個Button,將關閉彈出窗口ViewController並將其帶回前一個。這是一個screenshot。我遇到的問題是我添加了以下代碼:Xcode 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) 

    } 

而當我嘗試點擊關閉按鈕時,什麼也沒有發生。不知道我錯過了什麼。任何指導都會有所幫助。

這裏我把代碼爲兩個控制器:

JnsDetail.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) 
    } 

} 

JnsZoomController.swift

import Foundation 
import UIKit 

class JnsZoomController : UIViewController { 

    @IBOutlet var scrollView : UIScrollView! 
    @IBOutlet var jnsImageView : UIImageView! 
    @IBOutlet var jnsImageView2 : UIImageView! 
    @IBOutlet var jnsImageView3 : UIImageView! 

    var jsonextrct : JsonExtrct! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     if let imageData = jsonextrct.imageData { 

      let image = UIImage(data: imageData) 
      jnsImageView.image = UIImage(data: imageData) 
      //jnsImageView.bounds = CGRectMake(0, 0, image?.size.width, image?.size.height); 
     } 

     if let imageData2 = jsonextrct.imageData2 { 

      let image2 = UIImage(data: imageData2) 
      jnsImageView2.image = UIImage(data: imageData2) 
      //jnsImageView2.bounds = CGRectMake(0, 0, image?.size.width, image?.size.height); 
     } 

     if let imageData3 = jsonextrct.imageData3 { 

      let image3 = UIImage(data: imageData3) 
      jnsImageView3.image = UIImage(data: imageData3) 
      //jnsImageView3.bounds = CGRectMake(0, 0, image?.size.width, image?.size.height); 
     } 

     scrollView.contentSize = jnsImageView.frame.size 
     scrollView.contentSize = jnsImageView2.frame.size 
     scrollView.contentSize = jnsImageView3.frame.size 

    } 

    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) 

    } 
} 

回答

1

這裏的問題,因爲我看到它,如果你是「彈出」到根視圖控制器,這意味着你必須按下一個視圖co把手放在導航控制器的堆棧上,我沒有看到你將任何東西推到導航堆棧上。除非因爲某種原因蘋果公司決定停止推送視圖控制器,但我懷疑是這種情況。所以,我在代碼中看到的還有另一個問題。你被剛剛呈現視圖控制器PRESENTING視圖控制器,我沒有看到你在哪裏使用導航控制器SOOO呈現視圖控制器,如果調用

 self.navigationController?.popToRootViewControllerAnimated(true) 

再沒有什麼上是呈現一個視圖控制器導航控制器將從堆棧中移除的堆棧,因爲您在視圖控制器的導航控制器中不顯示模式而將模式化的視圖控制器呈現給另一個視圖控制器。

解決方案,也許,但這不是100%因爲我沒有你的代碼在我面前。

改變這樣的:

 self.navigationController?.popToRootViewControllerAnimated(true) 

到這樣的事情

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

我不做迅速,所以我的解決方案是僞代碼,隨意添加的問號,什麼不是蘋果由於某種原因決定了價值。

你也可以只改變你的演講到此:

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

再次,上面是僞代碼,但我想我把問號拿捏當場以便它做什麼它猜想做

此外,您還可以參考這個,雖然蘋果並沒有真正做告訴你一個非常徹底的工作了先進的導覽樹狀堆棧是如何工作的:

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

有時你需要有可能4-10導航控制器在同一時間運行的,所以一定要了解他們與視圖控制器如何相互作用,並確保你明白什麼持久性有機污染物,推,並提出做。祝你好運,祝你有美好的一天。

+1

我會檢查出來,你應該只使用

@IBAction func closeZoom(sender: AnyObject) { dismissViewControllerAnimated(true, completion: nil) } 

因爲你提出那個視圖控制器,該popToRootViewControllerAnimated(true)使用,看看是否有什麼東西我們缺失 – Loxx

+0

我現在看到的另一個問題可能是在視圖控制器底部的動畫中的這個「self.view.removeFromSuperview()」,我不會從視圖中刪除任何東西,除非您先關閉頂部的POP導航堆棧或DISMISS它。通過任何我的意思是視圖控制器 – Loxx

+0

對於我所看到的,他根本不使用'self.view.removeFromSuperview()',也許這是一種失敗的方法,但無論如何不要這樣做:P – dGambit

1

在closeZoom我認爲,當你推

+0

謝謝!去嘗試一下。 – wlmrlsda