美文网首页
iOS 获取对象的所有键值——KVO

iOS 获取对象的所有键值——KVO

作者: 可乐冒气 | 来源:发表于2017-11-15 17:07 被阅读0次

需求

有的时候我们需要更改系统类的某个属性时,例如 UIAlertControler 的文字大小,颜色,排列方式的时候,系统类有没有给出相应的属性或者方法进行修改,这就需要我们用KVO进行修改了,但是这个时候,又不知道具体的Key是什么 ,所以就有了如下的方法

1. 我们利用runtime 获取对象的所有属性 --- 引入 objc/runtime.h

#include <objc/runtime.h>

2.以系统对象为例,正常情况下我们无法查看系统对象的私有属性,如下方法可以得到其全部属性。

这里 我们以UINavigationBar为例

 unsigned int count;
    // 获取键值列表
    objc_property_t *propertyList = class_copyPropertyList([self.navigationController.navigationBar class], &count);
    for (unsigned int i = 0; i<count; i++) {
        const char *propertyName = property_getName(propertyList[i]);
        NSLog(@"property----="">%@", [NSString stringWithUTF8String:propertyName]);
    }

3 打印如下

2017-11-15 16:19:10.442611+0800 SchoolLife[4592:262991] property----=>hash
2017-11-15 16:19:10.442979+0800 SchoolLife[4592:262991] property----=>superclass
2017-11-15 16:19:10.443111+0800 SchoolLife[4592:262991] property----=>description
2017-11-15 16:19:10.443223+0800 SchoolLife[4592:262991] property----=>debugDescription
2017-11-15 16:19:10.443481+0800 SchoolLife[4592:262991] property----=>_backIndicatorLeftMargin
2017-11-15 16:19:10.444126+0800 SchoolLife[4592:262991] property----=>_barTranslucence
2017-11-15 16:19:10.444470+0800 SchoolLife[4592:262991] property----=>_overrideBackgroundExtension
2017-11-15 16:19:10.444937+0800 SchoolLife[4592:262991] property----=>_deferShadowToSearchBar
2017-11-15 16:19:10.445051+0800 SchoolLife[4592:262991] property----=>_animationIds
2017-11-15 16:19:10.445151+0800 SchoolLife[4592:262991] property----=>_startedAnimationTracking
2017-11-15 16:19:10.445265+0800 SchoolLife[4592:262991] property----=>_stack
2017-11-15 16:19:10.445343+0800 SchoolLife[4592:262991] property----=>_isContainedInPopover
2017-11-15 16:19:10.445415+0800 SchoolLife[4592:262991] property----=>_heightIncludingBackground
2017-11-15 16:19:10.445494+0800 SchoolLife[4592:262991] property----=>refreshControlHost
2017-11-15 16:19:10.445604+0800 SchoolLife[4592:262991] property----=>_wantsLargeTitleDisplayed
2017-11-15 16:19:10.445712+0800 SchoolLife[4592:262991] property----=>backgroundEffects
2017-11-15 16:19:10.445782+0800 SchoolLife[4592:262991] property----=>requestedContentSize
2017-11-15 16:19:10.445885+0800 SchoolLife[4592:262991] property----=>currentContentSize
2017-11-15 16:19:10.446106+0800 SchoolLife[4592:262991] property----=>_userContentGuide
2017-11-15 16:19:10.446302+0800 SchoolLife[4592:262991] property----=>state
2017-11-15 16:19:10.446472+0800 SchoolLife[4592:262991] property----=>locked
2017-11-15 16:19:10.446673+0800 SchoolLife[4592:262991] property----=>currentBackButton
2017-11-15 16:19:10.446846+0800 SchoolLife[4592:262991] property----=>currentLeftView
2017-11-15 16:19:10.447086+0800 SchoolLife[4592:262991] property----=>currentRightView
2017-11-15 16:19:10.447238+0800 SchoolLife[4592:262991] property----=>isMinibar
2017-11-15 16:19:10.447417+0800 SchoolLife[4592:262991] property----=>forceFullHeightInLandscape
2017-11-15 16:19:10.447661+0800 SchoolLife[4592:262991] property----=>_useInlineBackgroundHeightWhenLarge
2017-11-15 16:19:10.447865+0800 SchoolLife[4592:262991] property----=>_backgroundOpacity
2017-11-15 16:19:10.448059+0800 SchoolLife[4592:262991] property----=>_titleOpacity
2017-11-15 16:19:10.448247+0800 SchoolLife[4592:262991] property----=>alwaysUseDefaultMetrics
2017-11-15 16:19:10.448404+0800 SchoolLife[4592:262991] property----=>_hidesShadow
2017-11-15 16:19:10.448553+0800 SchoolLife[4592:262991] property----=>_wantsLetterpressContent
2017-11-15 16:19:10.448752+0800 SchoolLife[4592:262991] property----=>_backgroundView
2017-11-15 16:19:10.448946+0800 SchoolLife[4592:262991] property----=>_shadowAlpha
2017-11-15 16:19:10.449186+0800 SchoolLife[4592:262991] property----=>_disableBlurTinting
2017-11-15 16:19:10.449345+0800 SchoolLife[4592:262991] property----=>_backdropViewLayerGroupName
2017-11-15 16:19:10.449540+0800 SchoolLife[4592:262991] property----=>_requestedMaxBackButtonWidth
2017-11-15 16:19:10.449710+0800 SchoolLife[4592:262991] property----=>_accessibilityButtonBackgroundTintColor
2017-11-15 16:19:10.449902+0800 SchoolLife[4592:262991] property----=>barStyle
2017-11-15 16:19:10.450050+0800 SchoolLife[4592:262991] property----=>delegate
2017-11-15 16:19:10.450248+0800 SchoolLife[4592:262991] property----=>translucent
2017-11-15 16:19:10.450406+0800 SchoolLife[4592:262991] property----=>topItem
2017-11-15 16:19:10.450623+0800 SchoolLife[4592:262991] property----=>backItem
2017-11-15 16:19:10.450808+0800 SchoolLife[4592:262991] property----=>items
2017-11-15 16:19:10.451023+0800 SchoolLife[4592:262991] property----=>prefersLargeTitles
2017-11-15 16:19:10.451233+0800 SchoolLife[4592:262991] property----=>tintColor
2017-11-15 16:19:10.451460+0800 SchoolLife[4592:262991] property----=>barTintColor
2017-11-15 16:19:10.451633+0800 SchoolLife[4592:262991] property----=>shadowImage
2017-11-15 16:19:10.451816+0800 SchoolLife[4592:262991] property----=>titleTextAttributes
2017-11-15 16:19:10.452005+0800 SchoolLife[4592:262991] property----=>largeTitleTextAttributes
2017-11-15 16:19:10.452224+0800 SchoolLife[4592:262991] property----=>backIndicatorImage
2017-11-15 16:19:10.452398+0800 SchoolLife[4592:262991] property----=>backIndicatorTransitionMaskImage
2017-11-15 16:19:10.452574+0800 SchoolLife[4592:262991] property----=>hash
2017-11-15 16:19:10.452730+0800 SchoolLife[4592:262991] property----=>superclass
2017-11-15 16:19:10.453018+0800 SchoolLife[4592:262991] property----=>description
2017-11-15 16:19:10.453181+0800 SchoolLife[4592:262991] property----=>debugDescription
2017-11-15 16:19:10.453387+0800 SchoolLife[4592:262991] property----=>barPosition

相关文章

  • iOS 获取对象的所有键值——KVO

    需求 有的时候我们需要更改系统类的某个属性时,例如 UIAlertControler 的文字大小,颜色,排列方式的...

  • KVO

    KVO (Key-value-observing) 键值监听 iOS用什么方式实现对一个对象的KVO?(KVO的本...

  • KVO的本质

    面试问题: iOS用什么方式实现对一个对象的KVO? 如何手动触发KVO? KVO简介 KVO就是键值观测。有时候...

  • iOS原理(二)----KVO,KVC

    iOS原理(二)----KVO,KVC KVO KVO的全称是Key-Value Observing,俗称“键值监...

  • KVO

    KVO 简介 KVO 键值观察机制,就是观察指定对象的指定属性变化情况。 KVO 键值观察 依赖于 KVC 健值编...

  • iOS KVO

    什么是KVO KVO俗称“键值监听”,用来监听某个对象属性值的改变 KVO的使用 KVO 本质 在给某个对象添加K...

  • iOS - KVC

    KVC-键值编码KVC:对象取值或者设置值。KVO:监听对象值的变化。 获取对象值的优先级 OC对象的属性是由3部...

  • iOS 观察者KVO

    KVO 的基本概念(Key Value Observing) 基本概念键值观察是一种使对象获取其他对象的特定属性变...

  • iOS 面试知识点

    .1.数据的持久化:plist文件,对象归档,数据库,coredata 2.KVO:键值观察:是一种能使对象获取其...

  • OC中KVO的基本概念和使用方法

    基本概念 KVO(键值观察)是使用获取其他对象的特定属性变化的通知机制。控制器层的绑定技术就是严重依赖键值观察获得...

网友评论

      本文标题:iOS 获取对象的所有键值——KVO

      本文链接:https://www.haomeiwen.com/subject/eychvxtx.html