2014-09-28 57 views
0

环境:IOS 8.02 Xcode 6IOS 8 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 任何想法测试该如何实际禁用按钮?

回答

0

我发现这个问题,它实际上并不是按钮是问题所在。

Validate和Save按钮调用一个对Web服务执行异步调用的函数。

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

通知正在后台线程上发回。其影响是取消按钮被实时启用,但不是视觉状态。

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

0

还希望禁用按钮上的用户交互!

[self.btnCancel setUserInteractionEnabled:NO]; 
+0

我敢肯定,它的连线,否则它不会变成灰色。 – WilkieIT 2014-09-28 22:40:22

+0

@WilkieIT好的!然后尝试禁用按钮上的userInteraction,就像我在上面做的答案:)我更新了我的答案btw! – 2014-09-28 22:48:22

+0

我已经更新了描述并从表单后面提供了完整的代码。我也更新了代码,以符合你的建议,但仍然没有运气。 – WilkieIT 2014-09-29 10:06:23