2015-05-25 90 views
8

我知道那裏已經有一些關於這個問題,但我找不到解決方案。 有一個非常簡單的代碼,可以在我的一個項目上正常工作,但不會在另一個項目上工作:touchesBegan沒有在UIView子類中調用

這是UIView子類的代碼。

import UIKit 

class test : UIView { 

    init(frame: CGRect, color: UIColor) { 
     super.init(frame: frame) 
     self.backgroundColor = color 
     self.userInteractionEnabled = true 
    } 

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

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
     super.touchesBegan(touches, withEvent: event) 
     var alert = UIAlertView() 
     alert.title = "lol" 
     alert.message = "lol" 
     alert.addButtonWithTitle("Ok") 
     alert.show() 
    } 
} 

我確定解決方案很簡單,但我找不到它。

+0

「不工作」是什麼意思? – rdelmar

+0

我的意思是,當我觸摸視圖時,No AlertView顯示 –

+0

嘗試在函數中使用一個簡單的println()以查看它是否不是帶有alertview的錯誤? – Wraithseeker

回答

12

您需要確保在視圖的所有超級視圖上啓用了userInteractionEnabled。如果任何超級瀏覽器將此屬性設置爲false,則不會爲他們或他們的任何子視圖處理觸摸事件。

+2

沒有得到所需的結果,但我有view-> scrollview-> view layout,並且在這兩個視圖中,我已經將userInteractionEnabled設置爲true –

4

也許某些superviews將userInteractionEnabled設置爲「false」?

相關問題