IOS 8 xCode 6按钮显示为禁用但不是

时间:2014-09-28 20:28:30

标签: ios objective-c uibutton

环境:IOS 8.02 Xcode 6

我在StoryBoard上有一个表单。它有2个标签,一个文本框和2个按钮。 btnValidateAndSave禁用表单上的所有其他控件并调用Web服务。

btnCance变为灰色,好像它已被禁用但可点击。

以下是表单背后的实际代码。

#import "vcDigitalKeyManager.h"
#import "serviceManager.h"
#import "eziEnums.h"
#import "IsSystemLockedMethod.h"
#import "eziResponse.h"

@interface vcDigitalKeyManager ()
- (IBAction)btnValidateAndSave:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *txtDigitalKey;
@property (strong, nonatomic) IBOutlet UILabel *lblErrorMessage;
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *lblValidateActivity;
@property (strong, nonatomic) IBOutlet UIButton *btnValidateAndSave;
@property (strong, nonatomic) IBOutlet UIButton *btnCancel;
@property (strong, nonatomic) NSNumber *isWorking;

- (IBAction)btnCancel:(id)sender;

@end

@implementation vcDigitalKeyManager

@synthesize txtDigitalKey = _txtDigitalKey;
@synthesize lblErrorMessage = _lblErrorMessage;
@synthesize btnCancel = _btnCancel;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.lblValidateActivity setHidden:true];

}

- (IBAction)btnValidateAndSave:(id)sender {

    [self.txtDigitalKey resignFirstResponder];
    [self.txtDigitalKey setEnabled:NO];
    [self.txtDigitalKey setUserInteractionEnabled:NO];

    [self.lblValidateActivity setHidden:NO];

    [self.btnValidateAndSave setEnabled:NO];
    [self.btnValidateAndSave setUserInteractionEnabled:NO];

    [self.txtDigitalKey setEnabled:NO];
    [self.txtDigitalKey setUserInteractionEnabled:NO];

    self.btnCancel.enabled=NO;
    self.btnCancel.userInteractionEnabled = NO;

    [self.lblValidateActivity startAnimating];

    _isWorking = [NSNumber numberWithInt:1];

    NSString * requestBody = [serviceManager buildSoapMessage:IsSystemLocked :self.txtDigitalKey.text ];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(displayEziResponse:)
                                                 name:@"kIsSystemLockedResponseNotification"
                                               object:nil];

     [[[IsSystemLockedMethod alloc]init] callNonPciMethod:requestBody servicetype:NonPci];

}

- (void) displayEziResponse:(NSNotification *) notification{

    NSDictionary *userInfo = notification.userInfo;
    eziResponse *myResponse = [userInfo objectForKey:@"someKey"];

    if([myResponse.ErrorNumber isEqual:@"0"])
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
        [self.lblErrorMessage setText:[NSString stringWithFormat:@"%@: %@",myResponse.ErrorNumber, myResponse.ErrorMessage]];
        [self.lblErrorMessage setTextColor:[UIColor redColor]];
        [self.lblErrorMessage setHidden:NO];

        [self.lblValidateActivity stopAnimating];
        [self.lblValidateActivity setHidden:YES];

        [self.btnValidateAndSave setEnabled:YES];
        [self.btnValidateAndSave setUserInteractionEnabled:YES];

        [self.txtDigitalKey setEnabled:YES];
        [self.txtDigitalKey setUserInteractionEnabled:YES];

        [self.btnCancel setEnabled:YES];
        [self.btnCancel setUserInteractionEnabled:YES];

    }



}

- (IBAction)btnCancel:(id)sender {

    //NSLog([NSString stringWithFormat:@"%d",NSStringFromBOOL(BOOL self.btnCancel.enabled)]);

    if(_isWorking == [NSNumber numberWithInt:0])
    {
            [self dismissViewControllerAnimated:YES completion:nil];
    }
    //if(self.btnCancel.enabled)

}
@end

我在模拟器和iPhone 5S上测试了这个 任何想法如何实际禁用按钮?

2 个答案:

答案 0 :(得分:0)

首选也禁用按钮上的用户互动!

[self.btnCancel setUserInteractionEnabled:NO];

答案 1 :(得分:0)

我发现了问题,实际上并不是问题的按钮。

“验证并保存”按钮调用对Web服务执行异步调用的函数。

我正在使用通知让主线程知道何时收到响应。

通知正在后台线程上发回。这样做的结果是取消按钮实时启用,但不是视觉状态。

我修改了代码,以便将通知发送回主线程,现在主线程上的所有内容都会在1秒内更新。