无法启用用户与NSNotification的交互

时间:2013-03-25 09:30:48

标签: ios objective-c

我有一个按钮,当我点击它时设置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按钮...

3 个答案:

答案 0 :(得分: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]);
   }
}

答案 1 :(得分:0)

UIOperations必须在主线程中完成。

我怀疑你不在主线上。请通知方法

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

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

如果是问题,请点击链接

When responding to NSNotifications, what is the best practice for updating UIViews

答案 2 :(得分: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];
}
相关问题