2014-09-28 58 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