UIButton限制印刷机数量 [英] UIButton limiting number of presses

查看:75
本文介绍了UIButton限制印刷机数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,用户迅速按我的UIButton导致多个条目放置在我的数据库在线存储。我已经尝试过各种各样的,如隐藏的按钮,当它的动作被调用和某种切换,都没有成功。有没有反对限制新闻只有一个。

   - (IBAction)postData:(id)sender 
{

if(loginControl == 0)
{
if(nameIB.text.length&& numberIB.text.length> 0)
{
loginControl = 1;
loginButton.hidden = YES;

NSMutableData * data = [NSMutableData data];

NSString * number = numberIB.text;
NSString * name = nameIB.text;

NSString * nameString = [[NSString alloc] initWithFormat:@name =%@,name];
NSString * numberString = [[NSString alloc] initWithFormat:@& number =%@,number];
NSString * genderString = [[NSString alloc] initWithFormat:@& gender =%@,gender];

// NSLog(nameString);
// NSLog(numberString);

[data appendData:[nameString dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:[numberString dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:[genderString dataUsingEncoding:NSUTF8StringEncoding]];

NSURL * url = [NSURL URLWithString:@http://www.blah.net/blah.php];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@POST];
[request setHTTPBody:data];

NSURLResponse * response
NSError * err;
NSData * responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:& response error:& err];
NSLog(@responseData:%@,responseData);

userData = responseData;
[self startParsingUserId];

logoutButton.hidden = NO;
}
else {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@文本字段为空消息:@一个或多个文本字段为空委托:self cancelButtonTitle:@取消otherButtonTitles:nil];
[alert show];
[警报版本];

loginControl = 0;
}
}

}


方案

您应该立即设置启用隐藏

之后

时,此更改才会生效。画。代码是隐藏按钮,做东西,然后取消隐藏按钮,之前,按钮有机会重绘



你应该做的是设置 hidden 属性,然后开始计算(最好在后台线程)。一旦计算完成,您应该发信号通知主线程再次取消隐藏按钮。



如果你确定这只能在iOS 4.0+ ,你可以很容易地用大中央调度完成这个:

   - (IBAction)doStuff:(id)sender {
[button setEnabled:NO];
dispatch_async(dispatch_get_global_queue(0,0),^ {

//做所有的计算/同步请求
//这将发生在后台线程

dispatch_async(dispatch_get_main_queue(),^ {
[button setEnabled:YES];
});
});
}


I got an issue with users rapidly pressing my UIButton causing multiple entries being placed in my database stored online. I have tried all sorts such as hiding the button when it the action is called and some sort of toggle, both have been unsuccessful. Is there anyway to limit the press to just one. the action is linked to the touch up inside reference on the button.

-(IBAction)postData:(id)sender
{   

    if(loginControl == 0)
    {
    if(nameIB.text.length && numberIB.text.length > 0)
    {
        loginControl = 1;
        loginButton.hidden = YES;

        NSMutableData *data = [NSMutableData data]; 

        NSString *number = numberIB.text;
        NSString *name = nameIB.text;

        NSString *nameString = [[NSString alloc] initWithFormat:@"name=%@", name];
        NSString *numberString = [[NSString alloc] initWithFormat:@"&number=%@", number];
        NSString *genderString = [[NSString alloc] initWithFormat:@"&gender=%@", gender];

        //NSLog(nameString);
        //NSLog(numberString);

        [data appendData:[nameString dataUsingEncoding:NSUTF8StringEncoding]];
        [data appendData:[numberString dataUsingEncoding:NSUTF8StringEncoding]];
        [data appendData:[genderString dataUsingEncoding:NSUTF8StringEncoding]];

        NSURL *url = [NSURL URLWithString:@"http://www.blah.net/blah.php"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:data];

        NSURLResponse *response;
        NSError *err;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
        NSLog(@"responseData: %@", responseData);

        userData = responseData;
        [self startParsingUserId];

        logoutButton.hidden = NO;
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Text Fields Empty" message:@"One Or More Textfields Are Empty" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert show];
        [alert release];

        loginControl = 0;
    }
    }

}

解决方案

You should immediately be setting the enabled or hidden property of the button to disable interaction.

HOWEVER that change will not take effect until the next turn of the runloop, when everything gets re-drawn. As your code stands, your code is hiding the button, doing stuff, and then unhiding the button, all before the button gets a chance to redraw.

What you should do is set the hidden property and then start the computation (preferably on a background thread). Once and only once the computation completes, you should signal the main thread to un-hide the button again.

If you are OK with having this only work on iOS 4.0+, you can easily accomplish this with Grand Central Dispatch:

- (IBAction)doStuff:(id)sender {
  [button setEnabled:NO];
  dispatch_async(dispatch_get_global_queue(0,0), ^{

    // do all your computation/synchronous requesting here
    // this will happen on a background thread

    dispatch_async(dispatch_get_main_queue(), ^{
      [button setEnabled:YES];
    });
  });
}

这篇关于UIButton限制印刷机数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆