2015-03-19 13 views
10

मैं एक एनएसटीमर के उपयोगकर्ताइन्फो के साथ यूआईबटन पास करने की कोशिश कर रहा हूं। मैंने एनएसटीमीटर पर स्टैक ओवरफ्लो पर हर पोस्ट पढ़ी है। मैं बहुत करीब आ रहा हूं लेकिन वहां काफी कुछ नहीं मिल सकता है। इस पोस्ट मेंस्विफ्ट NSTimer userinfo

Swift NSTimer retrieving userInfo as CGPoint

func timeToRun(ButonToEnable:UIButton) { 
    var tempButton = ButonToEnable 
    timer = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: Selector("setRotateToFalse"), userInfo: ["theButton" :tempButton], repeats: false)  
} 

में मदद मिली है समारोह टाइमर चलाता

func setRotateToFalse() { 
    println( timer.userInfo)// just see whats happening 

    rotate = false 
    let userInfo = timer.userInfo as Dictionary<String, AnyObject> 
    var tempbutton:UIButton = (userInfo["theButton"] as UIButton) 
    tempbutton.enabled = true 
    timer.invalidate()  
} 

उत्तर

24

मुझे पता है आप इसे ठीक करने में कामयाब है, लेकिन मैंने सोचा कि मैं आप NSTimer उपयोग के बारे में कुछ और जानकारी देना होगा। टाइमर ऑब्जेक्ट तक पहुंचने का सही तरीका और इसलिए उपयोगकर्ता की जानकारी इसे नीचे की तरह उपयोग करना है। जब टाइमर initialising आप इस तरह यह बना सकते हैं:

स्विफ्ट 2.x

NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: Selector("setRotateToFalse:"), userInfo: ["theButton" :tempButton], repeats: false) 

स्विफ्ट 3.x <

Timer.scheduledTimer(timeInterval: 1, target: self, selector:#selector(ViewController.setRotateToFalse), userInfo: ["theButton" :tempButton], repeats: false) 

तो कॉलबैक इस तरह दिखता है:

func setRotateToFalse(timer:NSTimer) { 
    rotate = false 
    let userInfo = timer.userInfo as Dictionary<String, AnyObject> 
    var tempbutton:UIButton = (userInfo["theButton"] as UIButton) 
    tempbutton.enabled = true 
    timer.invalidate()  
} 

इसलिए आपको टाइमर का संदर्भ रखने की आवश्यकता नहीं है और अक्सर ग़लत वैश्विक चर से बचने की आवश्यकता नहीं है मुमकिन। यदि आपकी कक्षा NSObject से प्राप्त नहीं होती है, तो आप स्विफ्ट में किसी समस्या में भाग ले सकते हैं, जहां यह कहता है कि कोई कॉलबैक परिभाषित नहीं है लेकिन इसे फ़ंक्शन परिभाषा की शुरुआत में @objc जोड़कर आसानी से ठीक किया जा सकता है।

+0

हाय धन्यवाद कि बहुत मददगार – user2164327

+0

कोई समस्या नहीं है का उपयोग कर रहा है। जब आप एक पल हो तो क्या आप एक जवाब चिह्नित कर सकते हैं। चीयर्स –

+1

इसके अतिरिक्त, आपको पूरी तरह से 'userInfo' के रूप में एक शब्दकोश को पास नहीं करना है, आपको केवल एक ऑब्जेक्ट की आवश्यकता है जो' AnyObject 'के अनुरूप हो। व्यक्तिगत रूप से, मैंने इसे 'स्ट्रिंग' पास करने के लिए उपयोग किया – tfrank377

1

मैं बस के रूप में मैं इस पर पढ़ इससे पहले कि मैं पोस्ट इस पोस्ट करने के लिए जा रहा था। मैंने देखा कि मेरे पास userinfo से पहले timer.invalidate() था इसलिए यही काम नहीं कर रहा था। मैं इसे पोस्ट करूंगा क्योंकि यह किसी और की मदद कर सकता है।

func setRotateToFalse(timer:NSTimer) { 
    rotate = false 
    timer.invalidate() 
    let userInfo = timer.userInfo as Dictionary<String, AnyObject> 
    var tempbutton:UIButton = (userInfo["theButton"] as UIButton) 
    tempbutton.enabled = true 
} 
0

MacOS 10.12+ और iOS 10.0+ Timer के एक ब्लॉक आधारित एपीआई जो एक और अधिक सुविधाजनक तरीका

func timeToRun(buttonToEnable: UIButton) { 
    timer = Timer.scheduledTimer(withTimeInterval:4, repeats: false) { timer in 
     buttonToEnable.enabled = true 
    }  
} 

एक एक शॉट टाइमर स्वचालित रूप से अवैध हो जाएगा यह आग के बाद है प्रस्तुत करता है।


एक एक शॉट टाइमर के लिए एक समान सुविधाजनक तरीका GCD (DispatchQueue.main.asyncAfter)

func timeToRun(buttonToEnable: UIButton) { 
    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(4)) { 
     buttonToEnable.enabled = true 
    } 
} 
संबंधित मुद्दे