以UIAlertView為例棺蛛,在創(chuàng)建時(shí)凸丸,對(duì)事件進(jìn)行處理琼娘。
-
objc_setAssociatedObject
此方法以給定的鍵和策略為某對(duì)象設(shè)置關(guān)聯(lián)對(duì)象值
-
objc_getAssociatedObject
此方法根據(jù)給定的鍵從某對(duì)象中獲取相應(yīng)的關(guān)聯(lián)對(duì)象值
-
objc_removeAssociatedObjects
此方法移除指定對(duì)象的全部關(guān)聯(lián)對(duì)象
// 設(shè)置關(guān)聯(lián)對(duì)象時(shí),通常使用靜態(tài)全局變量做鍵朗伶。
static void * EOCMyAiertViewKey = "EOCMyAiertViewKey";
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Question" message:@"What do you want to do?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Continue", nil];
void(^ block)(NSInteger) = ^(NSInteger buttonIndex) {
// 在這里判斷ButtonIndex 做相應(yīng)處理
};
objc_setAssociatedObject(alert, EOCMyAiertViewKey, block, OBJC_ASSOCIATION_COPY);
[alert show];
#pragma mark - alertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
void(^ block)(NSInteger) = objc_getAssociatedObject(alertView, EOCMyAiertViewKey);
block(buttonIndex);
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者