将类设置为UITapGestureRecognizer的目标而不是“self”时应用程序崩溃

时间:2015-02-03 07:56:08

标签: ios swift uikit uitapgesturerecognizer ios8.1

当我将'self'设置为UITapGestureRecognizer实例的目标时,下面的代码有效,但当我将其设置为'SkinViewTransitionHelper'实例时,应用程序崩溃并且Xcode没有显示任何有用的信息,实际上什么也没有,何时应用程序崩溃控件(我的意思是绿色指示器)转到AppDelegate。此外,堆栈跟踪为空,只显示一些与崩溃无关的线程。我做了很多搜索并重读了UIGestureRecognizer的文档,但没有帮助。我究竟做错了什么?我正在使用Xcode版本6.2(6C107a)对应用程序进行编码。此外,我正在iOS模拟器版本8.2(553.8)中测试该应用程序。我也检查了UITapGestureRecognizer calling another class

/**
     Manages the view that contains logo and button.
     */
    class LaunchView: UIView {
        // MARK: Instance variables
        /** Logo image view */
        private var logoImageView:    UIImageView!
        /** x Button image view */
        private var xButtonImageView: UIImageView!

        // MARK: Designated Initializer

        /**
           Initialize LaunchView view with logo and button.

           :param: frame        Frame of this view
           :param: logoImage    Logo Image used at the top of the view
           :param: xButtonImage Button Image used at the bottom of the view
        */
        init(frame: CGRect, logoImage: UIImage, xButtonImage: UIImage){
            super.init(frame: frame)

            // Initialize the logoImageView
            logoImageView = UIImageView(frame: frame)
            // Set the image of logoImageView
            logoImageView.image = logoImage

            // Initialize the capriceButtonImage
            // Note: It's kinda hard coding the (x,y) values. Look for a generic way.
            xButtonImageView = UIImageView(frame: CGRect(x: frame.size.width / 4 + 20, y: frame.size.height - 200, width: xButtonImage.size.width, height: xButtonImage.size.height))
            // Set the image of xButtonImageView
            xButtonImageView.image = xButtonImage

            // SingleTap recognizer for xButtonImageView
            // Note: Find a generic solution instead of writing method name in "". It's kinda horrible; Maybe I forget to write the name of function correctly.
            // Note: Still not possible in Swift. reflect() helps if we want information about properties.
            // Note: https://github.com/ksm/SwiftInFlux#reflection
            // FIXME: App crashes using SkinViewTransitionHelper() as target
            let singleTapGestureRecognizer: UITapGestureRecognizer = 
                   UITapGestureRecognizer(target: SkinViewTransitionHelper(), action: "handleGesture:")

            // works like a charm if I remove the comment and comment above line
            // let singleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleGesture:")

            // Set the nunmber of taps required
            singleTapGestureRecognizer.numberOfTapsRequired = 1
            singleTapGestureRecognizer.numberOfTouchesRequired = 1

            // Add tap gesture to xButtonImageView

            xButtonImageView.addGestureRecognizer(singleTapGestureRecognizer)

            xButtonImageView.userInteractionEnabled = true

            // Add logoImageView to the view
            self.addSubview(logoImageView)
            // Add xButtonImageView to the view
            self.addSubview(xButtonImageView)
        }

        required init(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

        // func handleGesture(gestureRecongnizer: UIGestureRecognizer) {
            // if .Ended == gestureRecongnizer.state {
                // T0D0: Notify LaunchViewController of gesture
            // }
        // }
    }

    /**
    Handle gesture recognized by a UIView instance
    */
    class SkinViewTransitionHelper {
        // MARK: Gesture Handler

        /**
        Handle gesture recognized by a UIView instance
        */
        func handleGesture(gestureRecongnizer: UIGestureRecognizer) {
            println(__FUNCTION__)

            if .Ended == gestureRecongnizer.state {
                // TODO: Load SkinViewController
            }
        }
    }

import UIKit

class LaunchViewViewController: UIViewController {

    override func viewDidLoad() {
        println(self)
        println(__FUNCTION__)

       super.viewDidLoad()
    }

    override func loadView() {
        println(self)
        println(__FUNCTION__)

        // Set LaunchView as the view of LaunchViewViewController
        self.view = LaunchView(frame: UIScreen.mainScreen().bounds, logoImage: ImagesCatalog.Logo, xButtonImage: ImagesCatalog.xButton)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

2 个答案:

答案 0 :(得分:0)

崩溃是因为targetUITapGestureRecognizer类的弱属性,因此SkinViewTransitionHelper不会保留UITapGestureRecognizer的实例。

创建SKinViewTransitionHelper类型的属性并在其上存储实例并将其设置为UITapGestureRecognizer目标。

class LaunchView {

    var skinViewHelper : SKinViewTransitionHelper!

     init(frame: CGRect, logoImage: UIImage, xButtonImage: UIImage){

         //Your code
         self.skinViewHelper = SKinViewTransitionHelper()
         let singleTapGestureRecognizer: UITapGestureRecognizer = 
                   UITapGestureRecognizer(target: self.skinViewHelper, action: "handleGesture:")
     }
}

同时使用@objc

将要引用的功能标记为选择器
class SkinViewTransitionHelper {

    @objc func handleGesture(gestureRecongnizer: UIGestureRecognizer) {
        println(__FUNCTION__)

        if .Ended == gestureRecongnizer.state {
            // TODO: Load SkinViewController
        }
    }
}

答案 1 :(得分:0)

如果实例化SkinViewTransitionHelper将其值存储到变量(可能需要是类属性)然后将其指定为目标,会发生什么?

我认为,在'目标'之后,您可能会释放SkinViewTransitionHelper实例。赋值,因为在任何地方都没有硬引用。