NSTimer未正确执行

时间:2016-07-25 05:59:25

标签: ios swift timer nstimer

我正在开发一款需要大量用户输入的应用。因此,我正在尝试构建一个功能,不允许用户发布至少10秒的更多信息。这将有助于减少垃圾邮件发送者/巨魔/等。基本上发生的是当用户想要报告某些内容时,我的应用程序会测量用户与最近的其他报告位置之间的距离。如果超过200米并且在过去10秒内没有报告,那么他们可以自由发布。以下是完成此任务的代码:

if( distance > 200 && canPost){
        MainMap.addAnnotation(pointAnnotation)
        canPost = false
        sendTheData(String(locationManager.location?.coordinate.longitude), latitude: String(locationManager.location?.coordinate.latitude), time: getTime(), userId: "admin")
        let myTimer : NSTimer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: #selector(MapViewController.postDelayTimer), userInfo: nil, repeats: false)
        myTimer.fire()


    }else if(distance < 200 && canPost){
        let alertController = UIAlertController(title: "Whoa There!", message: "tOO clOSE!", preferredStyle: .Alert)
        let defaultAction = UIKit.UIAlertAction(title: "OK", style: .Default, handler: nil)
        alertController.addAction(defaultAction)
        self.presentViewController(alertController, animated: true, completion: nil)


    }

等等其他不同的实例。从计时器postDelayTimer调用的函数是:

func postDelayTimer() {
    canPost = true
}

很简单,但我显然在这里遗漏了一些东西。有人有想法吗?谢谢大家!

我期望发生的是用户在立即发布后无法再次发布,但他们能够连续发布。好像bool canPost总是如此。

3 个答案:

答案 0 :(得分:1)

您的代码是:

	<div class="nav">
		<ul>
			<li class="home"><a href="BraxtonTJohnsonHome.html"><img src="../pics/Logo.png" width="50" height="50"></a></li>
			<li class="about"><a href="BraxtonTJohnsonAbout.html">About</a></li>
			<li class="mywork"><a href="BraxtonTJohnsonMyWork.html">My Work</a></li>
			<li class="contact"><a class="active" href="BraxtonTJohnsonContact.html">Contact</a></li>
		</ul>
	</div>

	<div class="body">
  <!-- .row start -->
  <div class="row fixit">
  
		<div class="businessinquiry">
			<h2>Business Inquiries</h2>
			<p>Interested in working together? Fill out the simple form below with some more information about your project, and I'll be in touch.</p>
			<p>Don't be afraid to be honest with us about your timeline and your budget. We're upfront about our prices, and knowing beforehand how we can best help you will really help later on.</p>
		</div>
		<div class="contactquestion">
			<div class="words">
				<h2>General Questions & Inquiries</h2>

			</div>
			<form method="post" action="../php/index.php">
        
    			<input class="info" name="name" placeholder="What's your name?">
            
    			<input class="info" name="email" type="email" placeholder="What's your email?">
            
    			<textarea class="message" name="message" placeholder="How can I help?"></textarea>
            
    			<input class="info" id="submit" name="submit" type="submit" value="Submit">
        
			</form>
    
		</div>
     </div><!-- .row end -->
     
			<div class="social">
				<a class="facebook" href="https://www.facebook.com/bjohnson.rbi"><img src="../pics/facebook.png"></a>
				<a class="twitter" href="https://twitter.com/braxtontjohnson"><img src="../pics/twitter.png"></a>
				<a class="instagram" href="https://www.instagram.com/b_rax_johnson/"><img src="../pics/instagram.png"></a>
				<a class="youtube" href="https://www.youtube.com/user/RBIproductions"><img src="../pics/youtube.png"></a>
			</div>
      
	</div>

第二行不正确,请将其删除。您不想手动触发计时器。你想等待计时器在10秒后自行开启。

答案 1 :(得分:1)

两件事:

  1. 您可以在方法内创建计时器,将计时器作为类变量初始化并全局使用。类似的东西:

    class MapViewController: UIViewController {
        var myTimer = NSTimer()
    
        func activateTimer() {
          myTimer = NSTimer.scheduledTimerWithTimeInterval(10, target:self, selector: #MapViewController.postDelayTimer), userInfo: nil, repeats: true)
        }
    }
    
  2. myTimer.fire()是不必要的。定时器将在设置后触发。

答案 2 :(得分:0)

我通过在按下按钮后调用该函数来完成此操作。此函数的内部是NSTimer,当被触发时,继续调用第三个函数来负责查询所需的布尔值。