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變量過多帶來的問題。