Merge pull request #2081 from MattFoley/patch-1

Enum Constants Explanation
This commit is contained in:
Brent Vatne 2015-07-21 20:57:14 -07:00
commit 75b220d6ce
1 changed files with 41 additions and 0 deletions

View File

@ -230,6 +230,47 @@ 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
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, UIStatusBarAnimation) {
UIStatusBarAnimationNone,
UIStatusBarAnimationFade,
UIStatusBarAnimationSlide,
};
```
You must create a class extension of RCTConvert like so:
```objc
@implementation RCTConvert (StatusBarAnimation)
RCT_ENUM_CONVERTER(UIStatusBarAnimation, (@{ @"statusBarAnimationNone" : @(UIStatusBarAnimationNone),
@"statusBarAnimationFade" : @(UIStatusBarAnimationFade),
@"statusBarAnimationSlide" : @(UIStatusBarAnimationSlide),
UIStatusBarAnimationNone, integerValue)
@end
```
You can then define methods and export your enum constants like this:
```objc
- (NSDictionary *)constantsToExport
{
return @{ @"statusBarAnimationNone" : @(UIStatusBarAnimationNone),
@"statusBarAnimationFade" : @(UIStatusBarAnimationFade),
@"statusBarAnimationSlide" : @(UIStatusBarAnimationSlide) }
};
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.
## Sending Events to JavaScript