首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS UISwitch保持每4秒发送一次消息

iOS UISwitch保持每4秒发送一次消息
EN

Stack Overflow用户
提问于 2016-06-04 11:51:03
回答 3查看 87关注 0票数 0

我有个聊天应用,里面有个uiswitch。如果开关按钮打开,我希望应用程序每隔3秒连续发送一次"hi“,即使应用程序处于后台模式。我知道我可以使用NSTimer,但我不知道如何在这段代码中实现它(这是我第一次开发iOS应用程序)。请帮我弄一下这个。

我的代码是:

代码语言:javascript
运行
复制
// Allocate, initialize, and add the automatic button.
_AutomaticSend = [[UISwitch alloc] initWithFrame:CGRectMake([self width] - 50.0, textAreaY + Center(160.0, textAreaHeight), 50.0, 50.0)];
[_AutomaticSend addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];

代码语言:javascript
运行
复制
// Switch action
//NSUInteger counter = 0;

- (void)changeSwitch:(id)sender{

    if([sender isOn]){
        for (int a=1; a<=300; a++)
        {
            //[self performSelector:@selector(changeSwitch:) withObject:_textField afterDelay:70.0];
            [sender setOn:YES animated:YES];
            [_textField setText:@"hi"];
            NSString * text = [_textField text];

            // Update status.

            [[TSNAppContext singleton] updateStatus:text];

            // Add the status to the bubble.
            [self appendLocalPeerTableViewCellWithMessage:text];

        }


       // NSLog(@"Switch is ON");
    } else{
        NSLog(@"Switch is OFF");
    }

}

现在,在300个"hi“已经准备好显示之后,应用程序正在显示所有的"hi”。但我希望它能一个接一个地连续发送。

EN

回答 3

Stack Overflow用户

发布于 2016-06-04 12:14:18

  1. 在视图控制器中定义一个NSTimer实例和一个计数器:

@property (非原子,强)文本* timer;@property (非原子,赋值)更新定时器为fired:

  • (void)timerAction:(id)sender { _textField NSInteger text:@“hi”;NSString *text= _textField text;//更新状态时更新方法。[TSNAppContext singleton updateStatus:text];//给气泡添加状态self appendLocalPeerTableViewCellWithMessage: on:
    • (void)changeSwitch:(id)sender{;//发送300次后停止计时器1,if (self.counter += 300) { self.counter >=;self.timer = nil;} }
    1. Starts开关为on:
    • (void)changeSwitch:(id)sender{时计时器if ( self.counter +=){self.counter= 0;self.timer = NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES;} else { //当开关关闭时终止计时器无效;self.timer = nil;} }

然而,你不能让定时器在你的应用程序进入后台后无限期地继续工作(除了一些例外: VoIP,GPS应用程序等)。请参考官方文档Background Execution

票数 1
EN

Stack Overflow用户

发布于 2016-06-04 12:14:03

使用NSTimerDocumentation of NSTimer

.h文件中声明NSTimer的属性:

代码语言:javascript
运行
复制
@property (retain,nonatomic) NSTimer *myTimer;

- (void)changeSwitch:(id)sender
{
   if([sender isOn]){
   myTimer = [NSTimer scheduledTimerWithTimeInterval: 4 target: self
                                   selector: @selector(AddMessage:) userInfo: nil repeats: YES];

   } else{
    [self.timer invalidate];
   }
}

每隔4秒,定时器将调用以下函数:

代码语言:javascript
运行
复制
-(void) AddMessage:(NSTimer*) timer 
{ 
    //Add Message
}
票数 0
EN

Stack Overflow用户

发布于 2016-06-04 12:17:17

代码语言:javascript
运行
复制
//Nstimer *timer in .h

在UISwitch方法中,编写如下所示

代码语言:javascript
运行
复制
- (void)changeSwitch:(id)sender{

 if([sender isOn]){
timer=[NSTimer scheduledTimerWithTimeInterval:2.0
 target:self
 selector:@selector(targetMethod)
 userInfo:nil
 repeats:NO];
 }
else
{
    [timer invalidate];
}
}

像这样的targetMethod

代码语言:javascript
运行
复制
-(void)targetMethod
 {
[sender setOn:YES animated:YES];
[_textField setText:@"hi"];
NSString * text = [_textField text];

// Update status.

[[TSNAppContext singleton] updateStatus:text];

// Add the status to the bubble.
[self appendLocalPeerTableViewCellWithMessage:text];

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37626165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档