UIKit 框架,有这么一个神奇的东西:accessibilityIdentifier
//
// UIAccessibilityIdentification.h
// UIKit
//
// Copyright 2010-2012 Apple Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIView.h>
#import <UIKit/UIImage.h>
#import <UIKit/UIBarItem.h>
NS_ASSUME_NONNULL_BEGIN
@protocol UIAccessibilityIdentification <NSObject>
@required
/*
A string that identifies the user interface element.
default == nil
*/
@property(nullable, nonatomic, copy) NSString *accessibilityIdentifier NS_AVAILABLE_IOS(5_0);
@end
@interface UIView (UIAccessibility) <UIAccessibilityIdentification>
@end
@interface UIBarItem (UIAccessibility) <UIAccessibilityIdentification>
@end
/*
Defaults to the filename of the image, if available.
The default identifier for a UIImageView will be the identifier of its UIImage.
*/
@interface UIImage (UIAccessibility) <UIAccessibilityIdentification>
@end
NS_ASSUME_NONNULL_END
看注释:A string that identifies the user interface element.default ==nil
意思是说: accessibilityIdentifier是UI元素的一个NSString 标识,#默认值是nil!#有点类似Cell 的ReuseIdentifier了,这也就好理解了;
来个简单🌰:
UIImageView * picView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 90, 50, 50)];
picView.image = [UIImage imageNamed:@"add_pic.png"];
[picView.image setAccessibilityIdentifier:@"add”];
这个图片 “add_pic.png” 的标记 就是 “add” ,当你更换picview的image时,如果不给AccessibilityIdentifier属性重新复制的话,这个属性的值就会变成nil(默认),每个图片都会对应一个专属的AccessibilityIdentifier;方便我们识别图片。
if ([picView.image.accessibilityIdentifier isEqualToString:@"add"]) {
<#your code#>
}else{
<#your code#>
}
这样会方便很多,减少bool变量过多带来的问题。
网友评论