UIButton, KVC, KVO

Source: Internet
Author: User

Button
    • Custom button: Adjust the frame of the inner child control
      • Mode 1: Implement Titlerectforcontentrect: And Imagerectforcontentrect: Method, return the frame of Titlelabel and ImageView respectively
      • Method 2: Set in the Layoutsubviews method
    • Inner margin
// 设置按钮内容的内边距(影响到imageView和titleLabel)@property(nonatomic)          UIEdgeInsets contentEdgeInsets;// 设置titleLabel的内边距(影响到titleLabel)@property(nonatomic)          UIEdgeInsets titleEdgeInsets;// 设置imageView的内边距(影响到imageView)@property(nonatomic)          UIEdgeInsets imageEdgeInsets;
Picture stretching
    • Before IOS5
// 只拉伸中间的1x1区域- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
    • IOS5 start
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
Copy
    • There are 2 ways to implement a copy
      • Copy: Returns an immutable copy
      • Mutablecopy: Returns a mutable copy
    • Steps to implement a copy of a normal object
      • Compliance with Nscopying Protocol
      • Implementing-copywithzone: Methods
        • Create a new object
        • Assigning a value to a new object's properties
KVC
    • Full Name: Key value Coding (key code)
    • Assign value
// 能修改私有成员变量- (void)setValue:(id)value forKey:(NSString *)key;- (void)setValue:(id)value forKeyPath:(NSString *)keyPath;- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues;
    • Take value
// 能取得私有成员变量的值 - (id)valueForKey:(NSString *)key; - (id)valueForKeyPath:(NSString *)keyPath; - (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys;Dictionary Turn model
What is the type of dictionary value, and what type of property is given to the model
[Flag setvaluesforkeyswithdictionary:dict];
SETVALUESFORKEYSWITHDICTIONARY:KVC Dictionary Turn model
1. Traverse the dictionary all key Name,icon//2. Will go to the model to find, there is no corresponding property or method, if there is a property assignment [flag setvalue:@ "zhongguo.jpg" forkey:@ "icon"]
1. Find out if there is no SetIcon method, have, directly call method to assign value [flag seticon:@ "Zhongguo.jpg"]
2. Look for there is no icon attribute, there, direct assignment icon = icon;
3. Look for there is no _icon attribute, there, direct assignment _icon = icon;
4. Not found, direct error Setvalue:forundefinedkey:
The property name of the model must correspond to the dictionary key one by one KVO
    • Full Name: Key value observing (key monitoring)
    • Role: Changes in the property values of the listener model
    • Steps
      • Add Listener
        // 利用b对象来监听a对象name属性的改变[a addObserver:b forKeyPath:@"name" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"test"];
      • Implementing a listening method in the listener
        -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change     context:(void *)context{  NSLog(@"%@ %@ %@ %@", object, keyPath, change, context);}

UIButton, KVC, KVO

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.