mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-03-26 07:04:05 +08:00
Updating for Code Review
Using UIStatusBarAnimation for real world example
This commit is contained in:
@@ -230,25 +230,28 @@ console.log(CalendarManager.firstDayOfTheWeek);
|
||||
|
||||
Note that the constants are exported only at initialization time, so if you change `constantsToExport` values at runtime it won't affect the JavaScript environment.
|
||||
|
||||
###Enum Constants
|
||||
### Enum Constants
|
||||
|
||||
Enums that are defined via `NS_ENUM` cannot be used as method arguments without first extending RCTConvert.
|
||||
|
||||
In order to export the following `NS_ENUM` definition:
|
||||
|
||||
```objc
|
||||
typedef NS_ENUM(NSInteger, MyEnumType)
|
||||
{
|
||||
kEnumOne,
|
||||
kEnumTwo,
|
||||
typedef NS_ENUM(NSInteger, UIStatusBarAnimation) {
|
||||
UIStatusBarAnimationNone,
|
||||
UIStatusBarAnimationFade,
|
||||
UIStatusBarAnimationSlide,
|
||||
};
|
||||
```
|
||||
|
||||
You must create a class extension of RCTConvert like so:
|
||||
|
||||
```objc
|
||||
@implementation RCTConvert (MyEnumExtension)
|
||||
RCT_ENUM_CONVERTER(MyEnumType, (@{ @"enumOne" : @(kEnumOne),
|
||||
@"enumTwo" : @(kEnumTwo),
|
||||
kEnumOne, integerValue)
|
||||
@implementation RCTConvert (StatusBarAnimation)
|
||||
RCT_ENUM_CONVERTER(UIStatusBarAnimation, (@{ @"statusBarAnimationNone" : @(UIStatusBarAnimationNone),
|
||||
@"statusBarAnimationFade" : @(UIStatusBarAnimationFade),
|
||||
@"statusBarAnimationSlide" : @(UIStatusBarAnimationSlide),
|
||||
UIStatusBarAnimationNone, integerValue)
|
||||
@end
|
||||
```
|
||||
|
||||
@@ -257,12 +260,13 @@ You can then define methods and export your enum constants like this:
|
||||
```objc
|
||||
- (NSDictionary *)constantsToExport
|
||||
{
|
||||
return @{ @"enumOne" : @(kEnumOne),
|
||||
@"enumTwo" : @(kEnumTwo) }
|
||||
return @{ @"statusBarAnimationNone" : @(UIStatusBarAnimationNone),
|
||||
@"statusBarAnimationFade" : @(UIStatusBarAnimationFade),
|
||||
@"statusBarAnimationSlide" : @(UIStatusBarAnimationSlide) }
|
||||
};
|
||||
|
||||
RCT_EXPORT_METHOD(doSomethingWithEnum:(MyEnumType)enum
|
||||
completion:(RCTResponseSenderBlock)callback)
|
||||
RCT_EXPORT_METHOD(updateStatusBarAnimation:(UIStatusBarAnimation)animation
|
||||
completion:(RCTResponseSenderBlock)callback)
|
||||
```
|
||||
|
||||
Your enum will then be automatically unwrapped using the selector provided (`integerValue` in the above example) before being passed to your exported method.
|
||||
|
||||
Reference in New Issue
Block a user