Skip to content

Commit

Permalink
Merge pull request #2081 from MattFoley/patch-1
Browse files Browse the repository at this point in the history
Enum Constants Explanation
  • Loading branch information
brentvatne committed Jul 22, 2015
2 parents e3afc4e + 1e5f22d commit 75b220d
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/NativeModulesIOS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 75b220d

Please sign in to comment.