1

我有一个按钮,当我单击它时,它会设置 userInteraction:self.view.userInteractionEnabled = NO;这有效,如果我单击该按钮,我将无法再单击它。然后我得到了一个receiveNotification我在另一个班级打电话的。

- (void) receiveNotification:(NSNotification *) notification
{    
   if ([[notification name] isEqualToString:@"EnableUI"]){
       self.view.userInteractionEnabled = YES;
       NSLog(@"Successfully received the %@ notification!",[notification name]);
   }
}

程序输出告诉我收到通知,因为它打印出来Successfully received the EnableUI notification!但是,我仍然无法单击 UI 按钮...

4

3 回答 3

0

与其尝试在视图上设置 userInteraction(在这种情况下您似乎不需要),不如在按钮本身上设置交互?

。H

@property (strong, nonatomic) IBOutlet UIButton *buttonA;
@property (strong, nonatomic) IBOutlet UIButton *buttonB;
@property (strong, nonatomic) IBOutlet UIButton *buttonC;

.M

@synthesize buttonA, buttonB, buttonC;

- (void) receiveNotification:(NSNotification *) notification
{  
   NSArray *buttonArray = [[NSArray alloc] initWithObjects:buttonA,buttonB,buttonC,nil];  
   if ([[notification name] isEqualToString:@"EnableUI"]){
      for(UIButton* button in buttonArray) {
         UIButton *tempElement = [buttonArray objectAtIndex:i];
         tempElement.userInteractionEnabled = YES;
      }
      NSLog(@"Successfully received the %@ notification!",[notification name]);
   }
}
于 2013-03-25T09:34:04.267 回答
0

使用 UIButton.tag 标记应该禁用的按钮。

#define BUTTON_TAG_UNAUTH 123

- (void)disableButtonWithReason:(NSUInteger)reason
{
    NSArray *views = [self.view subviews];
    for (UIView *button in views) {
        //first checking tag cause more efficiently
        if (button.tag == reason && [button isKindOfClass:[UIButton class]]) {
            ((UIButton*)button).enabled = false;
        }
    }
}

- (void)disableButtonsDueToUnauthorized
{
    [self disableButtonWithReason:BUTTON_TAG_UNAUTH];
}
于 2014-03-11T13:28:36.287 回答
0

UIOperations 必须在主线程中完成。

我怀疑你不在主线程上。请在通知方法中查看

NSLog(@"%i", [NSThread isMainThread]);

如果不是请检查您是否在主线程上调用 postNotification 方法。

如果是问题,请点击链接

响应 NSNotifications 时,更新 UIViews 的最佳实践是什么

于 2013-03-25T09:36:49.200 回答