简体   繁体   English

在有条件的情况下禁用UIPickerView / UITableView的委托方法

[英]Disable delegate method for UIPickerView/UITableView on conditional case

I want to disable below delegate method for some condition 我想在某些情况下禁用下面的委托方法

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

let say if(x==YES) then only above delegate method hit else it won't respond. 假设if(x==YES)则只有上面的委托方法命中,否则它将不会响应。

Please let me know your suggestion. 请让我知道您的建议。

Override respondsToSelector: and return NO in case of You does not want to call this method: 覆盖respondsToSelector:如果您不想调用此方法,则返回NO

- (BOOL)respondsToSelector:(SEL)selector
{
    if (selector == @selector(pickerView:viewForRow:forComponent:reusingView:))
    {
        if(your_Condition)
        {
           return YES;
        }
        eles
        {
           return NO;
        }
    }  
    return [super respondsToSelector:selector];
}

So you're implementing the delegate method as normally. 因此,您将照常实现委托方法。 When the picker view is asking your delegate if it understands the message you're simply lying to it. 当选择器视图询问您的代表是否理解该消息时,您只是在撒谎。

There are two ways you can do it. 有两种方法可以做到这一点。

1) Make Delegate Null 1)使代表为空

Set a condition when you do not want to call the method and at that place make delegate = nill . 当您不想调用该方法时,请设置一个条件,然后在该位置执行delegate = nill

And again Reset it delegate =self when needs to call the method. 然后在需要调用方法时再次将其delegate =self重置。

The second method which I must say Better one 我必须说的第二种方法更好的一种

2) Use flag inside the Delegate method 2)在Delegate方法内使用标志

Try to implement if-condition (Flag) inside the method and set its TRUE and FALSE values as per requirement. 尝试在方法内部实现if-condition(Flag),并根据要求设置其TRUE和FALSE值。

for ex. 对于前。

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
     if(x == YES) {
                // Only this code need to implement
     } else {
     return;
     }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM