从UIViewCell

时间:2017-11-22 07:46:09

标签: xcode

//在UITableViewCell中 //当我在屏幕上显示时,它会显示"打开图像 2017-11-22 13:15:31.023129 + 0530 chat [1376:265960]警告:尝试在窗口层次结构中显示其视图!"

func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
    {
        let tappedImage = tapGestureRecognizer.view as! UIImageView
        print("open image")
        let sb =  UIStoryboard.init(name: "Main", bundle: nil)
        let vc : openimageinfullscreen = sb.instantiateViewController(withIdentifier: "openimageinfullscreen") as! openimageinfullscreen
        self.window?.rootViewController?.present(vc, animated: true, completion: nil)
    }

1 个答案:

答案 0 :(得分:0)

您需要获得顶级呈现的视图控制器然后呈现。 试试这个。

func topViewController() -> UIViewController? {
    if var topController = UIApplication.shared.keyWindow?.rootViewController {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
        return topController
    }
    return nil
}


func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
    let tappedImage = tapGestureRecognizer.view as! UIImageView
    print("open image")
    let sb =  UIStoryboard.init(name: "Main", bundle: nil)
    let vc : openimageinfullscreen = sb.instantiateViewController(withIdentifier: "openimageinfullscreen") as! openimageinfullscreen
    self.topViewController()?.present(vc, animated: true, completion: nil)
}

另请注意,您不应该使用小写字母开头。 openimageinfullscreen类应为OpenImageInFullscreen