11var 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
87127JSB . exports = ButtonsViewController ;
0 commit comments