Skip to content

Commit 16f1cbc

Browse files
Update example project.
1 parent b8914b0 commit 16f1cbc

9 files changed

Lines changed: 342 additions & 45 deletions

File tree

Classes/JSBScriptingSupport.m

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
static JSContext *globalContext;
1616

17+
#pragma mark -
18+
1719
static NSString *mangledNameFromClass(Class cls)
1820
{
1921
return [NSString stringWithFormat:@"__JSB_%@", NSStringFromClass(cls)];
@@ -212,10 +214,12 @@ static void forwardInvocation(id self, SEL _cmd, NSInvocation *invocation)
212214
NSString *propertyName = propertyNameFromSelector(invocation.selector);
213215
JSValue *value = globalContext[mangledNameFromClass([self class])][@"instanceMembers"][propertyName];
214216

215-
NSArray *arguments = extractArguments(invocation);
216-
JSValue *returnValue = [value callWithArguments:arguments];
217-
218-
setReturnValue(returnValue, invocation);
217+
if (!value.isUndefined) {
218+
NSArray *arguments = extractArguments(invocation);
219+
JSValue *returnValue = [value callWithArguments:arguments];
220+
221+
setReturnValue(returnValue, invocation);
222+
}
219223

220224
context[@"self"] = [NSNull null];
221225
}
@@ -242,6 +246,33 @@ static BOOL respondsToSelector(id self, SEL _cmd, SEL selector)
242246
return !value.isUndefined;
243247
}
244248

249+
#pragma mark -
250+
251+
@interface UIControl (JavaScriptBridge)
252+
253+
@end
254+
255+
@implementation UIControl (JavaScriptBridge)
256+
257+
- (void)__addTarget:(id)target action:(NSString *)action forControlEvents:(UIControlEvents)controlEvents
258+
{
259+
[self addTarget:target action:NSSelectorFromString(action) forControlEvents:controlEvents];
260+
}
261+
262+
- (void)__removeTarget:(id)target action:(NSString *)action forControlEvents:(UIControlEvents)controlEvents
263+
{
264+
[self removeTarget:target action:NSSelectorFromString(action) forControlEvents:controlEvents];
265+
}
266+
267+
- (void)__sendAction:(NSString *)action to:(id)target forEvent:(UIEvent *)event
268+
{
269+
[self sendAction:NSSelectorFromString(action) to:target forEvent:event];
270+
}
271+
272+
@end
273+
274+
#pragma mark -
275+
245276
@implementation JSBScriptingSupport
246277

247278
+ (void)initialize
@@ -268,6 +299,9 @@ + (void)initialize
268299
@" return __JSB_JSBScriptingSupport.require(name);\n"
269300
@" },\n"
270301
@" exports: {},\n"
302+
@" selector: function(str) {\n"
303+
@" return __JSB_JSBScriptingSupport.selectorFromString(str);\n"
304+
@" },\n"
271305
@" dump: function(obj) {\n"
272306
@" return __JSB_JSBScriptingSupport.dump(obj);\n"
273307
@" }\n"
@@ -345,6 +379,8 @@ + (id)defineClass:(NSString *)declaration
345379

346380
+ (id)require:(NSString *)name
347381
{
382+
JSValue *module = nil;
383+
348384
NSBundle *mainBundle = [NSBundle mainBundle];
349385
NSString *path = [mainBundle pathForResource:name ofType:@"js"];
350386

@@ -358,15 +394,24 @@ + (id)require:(NSString *)name
358394
@" %@\n"
359395
@"})();\n", script];
360396
[globalContext evaluateScript:closure];
361-
JSValue *module = globalContext[@"JSB"][@"exports"];
362-
return module;
397+
module = globalContext[@"JSB"][@"exports"];
363398
}
364399

365-
return nil;
400+
return module;
366401
}
367402

368403
#pragma mark - for debug
369404

405+
+ (void)log:(NSString *)format, ...
406+
{
407+
va_list args;
408+
va_start(args, format);
409+
NSString *result = [[NSString alloc] initWithFormat:format arguments:args];
410+
va_end(args);
411+
412+
NSLog(@"%@", result);
413+
}
414+
370415
+ (void)dump:(id)object
371416
{
372417
NSLog(@"%@", object);

Classes/ios/FrameworkSupport/UIKit/JSBUIControl.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,19 @@
2525
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;
2626
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;
2727
- (void)cancelTrackingWithEvent:(UIEvent *)event;
28-
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
29-
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
28+
JSExportAs(addTargetActionForControlEvents,
29+
- (void)__addTarget:(id)target action:(NSString *)action forControlEvents:(UIControlEvents)controlEvents
30+
);
31+
JSExportAs(removeTargetActionForControlEvents,
32+
- (void)__removeTarget:(id)target action:(NSString *)action forControlEvents:(UIControlEvents)controlEvents
33+
);
34+
- (void)removeTarget:(id)target action:(NSString *)action forControlEvents:(UIControlEvents)controlEvents;
3035
- (NSSet *)allTargets;
3136
- (UIControlEvents)allControlEvents;
3237
- (NSArray *)actionsForTarget:(id)target forControlEvent:(UIControlEvents)controlEvent;
33-
- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event;
38+
JSExportAs(sendActionToForEvent,
39+
- (void)__sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
40+
);
3441
- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents;
3542

3643
#pragma clang diagnostic pop

Examples/UICatalog/UICatalog.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
/* Begin PBXBuildFile section */
1010
149AE1091876E8BC00018A8C /* buttonsViewController.js in Resources */ = {isa = PBXBuildFile; fileRef = 149AE1081876E8BC00018A8C /* buttonsViewController.js */; };
1111
149AE10B1876F32D00018A8C /* mainViewController.js in Resources */ = {isa = PBXBuildFile; fileRef = 149AE10A1876F32D00018A8C /* mainViewController.js */; };
12+
149B8722187714BB0074B480 /* controlsViewController.js in Resources */ = {isa = PBXBuildFile; fileRef = 149B8721187714BB0074B480 /* controlsViewController.js */; };
13+
149B87241877199C0074B480 /* webViewController.js in Resources */ = {isa = PBXBuildFile; fileRef = 149B87231877199C0074B480 /* webViewController.js */; };
1214
149C53A51875C5870003EE16 /* JSBScriptingSupport.m in Sources */ = {isa = PBXBuildFile; fileRef = 149C53A21875C5870003EE16 /* JSBScriptingSupport.m */; };
1315
149C53A61875C5870003EE16 /* JSContext+Helper.m in Sources */ = {isa = PBXBuildFile; fileRef = 149C53A41875C5870003EE16 /* JSContext+Helper.m */; };
1416
149C56691875C5BB0003EE16 /* JSBAVFoundation.m in Sources */ = {isa = PBXBuildFile; fileRef = 149C53DC1875C5B90003EE16 /* JSBAVFoundation.m */; };
@@ -55,6 +57,8 @@
5557
/* Begin PBXFileReference section */
5658
149AE1081876E8BC00018A8C /* buttonsViewController.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = buttonsViewController.js; sourceTree = "<group>"; };
5759
149AE10A1876F32D00018A8C /* mainViewController.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = mainViewController.js; sourceTree = "<group>"; };
60+
149B8721187714BB0074B480 /* controlsViewController.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = controlsViewController.js; sourceTree = "<group>"; };
61+
149B87231877199C0074B480 /* webViewController.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = webViewController.js; sourceTree = "<group>"; };
5862
149C539F1875C5870003EE16 /* JavaScriptBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JavaScriptBridge.h; path = ../../../Classes/JavaScriptBridge.h; sourceTree = "<group>"; };
5963
149C53A01875C5870003EE16 /* JSBNSObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JSBNSObject.h; path = ../../../Classes/JSBNSObject.h; sourceTree = "<group>"; };
6064
149C53A11875C5870003EE16 /* JSBScriptingSupport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JSBScriptingSupport.h; path = ../../../Classes/JSBScriptingSupport.h; sourceTree = "<group>"; };
@@ -1749,6 +1753,8 @@
17491753
14B5CD4E1875C3FF0019A135 /* main.js */,
17501754
149AE10A1876F32D00018A8C /* mainViewController.js */,
17511755
149AE1081876E8BC00018A8C /* buttonsViewController.js */,
1756+
149B8721187714BB0074B480 /* controlsViewController.js */,
1757+
149B87231877199C0074B480 /* webViewController.js */,
17521758
14B5CD311875C3740019A135 /* Images.xcassets */,
17531759
14B5CD261875C3740019A135 /* Supporting Files */,
17541760
149C53A71875C59C0003EE16 /* Vendor */,
@@ -1821,6 +1827,8 @@
18211827
14B5CD4F1875C3FF0019A135 /* main.js in Resources */,
18221828
149AE10B1876F32D00018A8C /* mainViewController.js in Resources */,
18231829
149AE1091876E8BC00018A8C /* buttonsViewController.js in Resources */,
1830+
149B8722187714BB0074B480 /* controlsViewController.js in Resources */,
1831+
149B87241877199C0074B480 /* webViewController.js in Resources */,
18241832
14B5CD2A1875C3740019A135 /* InfoPlist.strings in Resources */,
18251833
14B5CD321875C3740019A135 /* Images.xcassets in Resources */,
18261834
);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"scale" : "1x",
6+
"filename" : "UIButton_custom.png"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
}
12+
],
13+
"info" : {
14+
"version" : 1,
15+
"author" : "xcode"
16+
}
17+
}
655 Bytes
Loading
Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,78 @@
11
var ButtonsViewController = JSB.define('ButtonsViewController : UITableViewController', {
22
viewDidLoad: function() {
3-
self.navigationItem.title = 'Buttons';
3+
self.newButton = function(title, target, selector, frame, image, imagePressed, darkTextColor) {
4+
var button = UIButton.alloc().initWithFrame(frame);
5+
6+
button.contentVerticalAlignment = 0;
7+
button.contentHorizontalAlignment = 0;
8+
9+
button.setTitleForState(title, 0);
10+
if (darkTextColor) {
11+
button.setTitleColorForState(UIColor.blackColor(), 0);
12+
} else {
13+
button.setTitleColorForState(UIColor.whiteColor(), 0);
14+
}
15+
16+
var newImage = image.stretchableImageWithLeftCapWidthTopCapHeight(12, 0);
17+
button.setBackgroundImageForState(newImage, 0);
18+
19+
var newPressedImage = imagePressed.stretchableImageWithLeftCapWidthTopCapHeight(12, 0);
20+
button.setBackgroundImageForState(newPressedImage, 1 << 0);
21+
22+
button.backgroundColor = UIColor.clearColor();
423

5-
this.dataSourceArray = [];
24+
button.addTargetActionForControlEvents(target, selector, 1 << 6);
625

7-
var buttonBackground = UIImage.imageNamed('whiteButton');
8-
var buttonBackgroundPressed = UIImage.imageNamed('blueButton');
26+
return button;
27+
};
928

10-
var frame = {x: 0, y: 5, width: 106, height: 40};
29+
self.grayButton = function() {
30+
var buttonBackground = UIImage.imageNamed('whiteButton');
31+
var buttonBackgroundPressed = UIImage.imageNamed('blueButton');
1132

12-
var button = UIButton.alloc().initWithFrame(frame);
33+
var frame = {x: 0, y: 5, width: 106, height: 40};
34+
var button = self.newButton('Gray', self, 'action:', frame, buttonBackground, buttonBackgroundPressed, true);
1335

14-
button.contentVerticalAlignment = 0;
15-
button.contentHorizontalAlignment = 0;
16-
button.setTitleForState('Gray', 0);
17-
button.setTitleColorForState(UIColor.blackColor(), 0);
36+
button.tag = 1;
1837

19-
var newImage = buttonBackground.stretchableImageWithLeftCapWidthTopCapHeight(12, 0);
20-
button.setBackgroundImageForState(newImage, 0);
38+
return button;
39+
};
2140

22-
var newPressedImage = buttonBackgroundPressed.stretchableImageWithLeftCapWidthTopCapHeight(12, 0);
23-
button.setBackgroundImageForState(newPressedImage, 1 << 0);
41+
self.imageButton = function() {
42+
var buttonBackground = UIImage.imageNamed('whiteButton');
43+
var buttonBackgroundPressed = UIImage.imageNamed('blueButton');
2444

25-
button.backgroundColor = UIColor.clearColor();
26-
button.tag = 1;
45+
var frame = {x: 0, y: 5, width: 106, height: 40};
46+
47+
var button = self.newButton('', self, 'action:', frame, buttonBackground, buttonBackgroundPressed, true);
48+
button.setImageForState(UIImage.imageNamed('UIButton_custom'), 0);
2749

28-
dataSourceArray.push({
50+
return button;
51+
}
52+
53+
self.navigationItem.title = 'Buttons';
54+
55+
self.dataSourceArray = [{
2956
sectionTitleKey: 'UIButton',
3057
labelKey: 'Background Image',
3158
sourceKey: 'ButtonsViewController.m:\r(UIButton *)grayButton',
32-
viewKey: button
33-
});
59+
viewKey: self.grayButton()
60+
},
61+
{
62+
sectionTitleKey: 'UIButton',
63+
labelKey: 'Button with Image',
64+
sourceKey: 'ButtonsViewController.m:\r(UIButton *)imageButton',
65+
viewKey: self.imageButton()
66+
}];
3467
},
3568
numberOfSectionsInTableView: function(tableView) {
36-
return dataSourceArray.length;
69+
return self.dataSourceArray.length;
3770
},
3871
tableViewNumberOfRowsInSection: function(tableView, section) {
3972
return 2;
4073
},
4174
tableViewTitleForHeaderInSection: function(tableView, section) {
42-
return dataSourceArray[section]['sectionTitleKey'];
75+
return self.dataSourceArray[section]['sectionTitleKey'];
4376
},
4477
tableViewHeightForRowAtIndexPath: function(tableView, indexPath) {
4578
return (indexPath.row == 0) ? 50 : 38;
@@ -55,8 +88,8 @@ var ButtonsViewController = JSB.define('ButtonsViewController : UITableViewContr
5588
viewToRemove.removeFromSuperview();
5689
}
5790

58-
cell.textLabel.text = dataSourceArray[indexPath.section]['labelKey'];
59-
var button = dataSourceArray[indexPath.section]['viewKey'];
91+
cell.textLabel.text = self.dataSourceArray[indexPath.section]['labelKey'];
92+
var button = self.dataSourceArray[indexPath.section]['viewKey'];
6093

6194
var newFrame = button.frame;
6295
newFrame.x = cell.contentView.frame.width - newFrame.width - 10;
@@ -77,11 +110,18 @@ var ButtonsViewController = JSB.define('ButtonsViewController : UITableViewContr
77110
cell.textLabel.numberOfLines = 2;
78111
cell.textLabel.highlightedTextColor = UIColor.blackColor();
79112
cell.textLabel.font = UIFont.systemFontOfSize(12);
80-
cell.textLabel.text = menuList[indexPath.section]['sourceKey'];
113+
cell.textLabel.text = self.dataSourceArray[indexPath.section]['sourceKey'];
81114

82115
return cell;
83116
}
84-
}
117+
},
118+
action: function(sender) {
119+
// var alertView = UIAlertView.alloc().initWithTitleMessageDelegateCancelButtonTitleOtherButtonTitles('Alert', 'Button pushed.', null, 'Cancel', 'OK', null);
120+
var alertView = UIAlertView.new();
121+
alertView.message = 'Button pushed.';
122+
alertView.addButtonWithTitle('OK');
123+
alertView.show();
124+
}
85125
});
86126

87127
JSB.exports = ButtonsViewController;

0 commit comments

Comments
 (0)