簡體   English   中英

KVO 檢查目標 c 中 UIView 中所有子視圖的 clipsToBounds 的變化

[英]KVO check for change of clipsToBounds of all subviews in an UIView in objective c

我正在嘗試為我的 UIView 中所有子視圖的 clipsToBounds 屬性實現一個KVO示例。 我不太明白如何更改 observeValueForKeyPath 方法中的值。 我正在使用這段代碼:

-(void)ViewDidLoad{
        [self.navigationController.view addObserver:self forKeyPath:@"clipsToBounds" options:NSKeyValueObservingOptionNew |
         NSKeyValueObservingOptionOld context:nil];
    }
    
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSLog(@"Triggered...")
    }

當我更改存在於 UIView 中的子視圖的屬性 clipToBounds 時,它會被觸發。 對於發生的每個觸發器,我都需要將值更改回 false。 我應該在 observeValueForKeyPath 中寫什么來更改 clipsToBounds 屬性? 任何幫助表示贊賞。

當然,添加觀察者必須在它起作用之前完成。 永遠不會調用您在“ViewDidLoad”中的拼寫錯誤,因為它應該是“viewDidLoad”。 除此之外,您的 KVO 模式可能看起來像..

static void *kvoHelperClipsToBounds = &kvoHelperClipsToBounds;

-(void)viewDidLoad {
    [self.navigationController.view addObserver:self forKeyPath:@"clipsToBounds" options:NSKeyValueObservingOptionNew context:kvoHelperClipsToBounds];
}
    
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (context == kvoHelperClipsToBounds) {
        NSLog(@"context compare Triggered...");
    } 

    // or compare against the keyPath
    else if ([keyPath isEqualToString:@"clipsToBounds"]) {
        NSLog(@"classic Key compare Triggered...");
    }
    else
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}

旁注:上下文只需要唯一即可將其與其他 KVO 區分開來。它也可以是對真實對象指針的引用。

不要忘記添加的觀察者必須在釋放之前被移除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM