小秋的家

          home

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            1 Posts :: 91 Stories :: 17 Comments :: 0 Trackbacks

          (轉載請保留此文字:本文來源:[iphone開發私房菜_2_] 為UIToolBar工具條添加各種各樣的UIBbarButtonItem http://blog.csdn.net/ipromiseu/archive/2010/12/16/6080689.aspx] write by Gray.Luo guohui.great@gmail.com)

           

          1.首先,我們看一下UIBbarButtonItem有哪些初始化方法,這也可以看出,它可以被定義為什么東東,然后加到UIToolBar上面去。

          根據SDK的文檔,我們可以發現UIBbarButtonItem有如下幾種初始化的方法:

          -initWithTitle

          -initWithImage

          -initWithBarButtonSystemItem

          -initWithCustomView

           

          第4種方法就是我們添加各種作料的接口,所以今天的主角其它也是它。

           

          2.在UIToolBar上面添加Title

           

           

          1. UIToolbar *myToolBar = [[UIToolbar alloc] initWithFrame:  
          2.                                                     CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];  
          3.                                                       
          4. NSMutableArray *myToolBarItems = [NSMutableArray array];  
          5. [myToolBarItems addObject:[[[UIBarButtonItem alloc]  
          6.                                                         initWithTitle:@"myTile"   
          7.                                                         style:UIBarButtonItemStylePlain   
          8.                                                         target:self   
          9.                                                         action:@selector(action)] autorelease]];  
          10. [myToolBar setItems:myToolBarItems animated:YES];  
          11. [myToolBar release];  
          12. [myToolBarItems];                                                         
           

           

          setItems傳入值或者說items是一個對象數組。

           

          3.在UIToolBar上面添加image

           

          1. [myToolBarItems addObject:[[[UIBarButtonItem alloc]  
          2.                                         initWithImage:[UIImage imageNamed:@"myImage.png"]   
          3.                                         style:UIBarButtonItemStylePlain   
          4.                                         target:self   
          5.                                         action:@selector(action)]];  
           

           

          4.在UIToolBar上面添加SystemItem

          1. [myToolBarItems addObject:[[[UIBarButtonItem alloc]  
          2.                                         initWithBarButtonSystemItem:UIBarButtonSystemItemPlay   
          3.                                         target:self   
          4.                                         action:@selector(action)] autorelease]];  
           

           

          Note:

          initWithBarButtonSystemItem初始化:

          - (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action

          Defines system defaults for commonly used items.

          1. typedef enum {  
          2.     UIBarButtonSystemItemDone,  
          3.     UIBarButtonSystemItemCancel,  
          4.     UIBarButtonSystemItemEdit,  
          5.     UIBarButtonSystemItemSave,  
          6.     UIBarButtonSystemItemAdd,  
          7.     UIBarButtonSystemItemFlexibleSpace,  
          8.     UIBarButtonSystemItemFixedSpace,  
          9.     UIBarButtonSystemItemCompose,  
          10.     UIBarButtonSystemItemReply,  
          11.     UIBarButtonSystemItemAction,  
          12.     UIBarButtonSystemItemOrganize,  
          13.     UIBarButtonSystemItemBookmarks,  
          14.     UIBarButtonSystemItemSearch,  
          15.     UIBarButtonSystemItemRefresh,  
          16.     UIBarButtonSystemItemStop,  
          17.     UIBarButtonSystemItemCamera,  
          18.     UIBarButtonSystemItemTrash,  
          19.     UIBarButtonSystemItemPlay,  
          20.     UIBarButtonSystemItemPause,  
          21.     UIBarButtonSystemItemRewind,  
          22.     UIBarButtonSystemItemFastForward,  
          23.     UIBarButtonSystemItemUndo,        // iPhoneOS 3.0  
          24.     UIBarButtonSystemItemRedo,        // iPhoneOS 3.0  
          25. } UIBarButtonSystemItem;  
           

           

           

          5.在UIToolBar上面添加其它各種控件,最自由意義,最有意思的,我把它放在最后來講。我們使用initWithCustomView來完成,

          這里需要看一下initWithCustomView的定義:

          - (id)initWithCustomView:(UIView *)customView

          可以看出,它的參數是一個VIEW,所以我們給它的配料要正確哦才行哦,否則,你就等著時間DIDADIDA的流失吧.

          A>加一個開關switch:

          1. [myToolBarItems addObject:[[[UIBarButtonItem alloc]   
          2.                                     initWithCustomView:[[[UISwitch alloc] init] autorelease]]  
          3.                                     autorelease]];  
           

           

          B>加一個按鈕UIBarButtonItem

          1. UIBarButtonItem *myButton = [[[UIBarButtonItem alloc]  
          2.                                  initWithTitle:@"myButton"  
          3.                                  style:UIBarButtonItemStyleBordered  
          4.                                  target:self   
          5.                                  action:@selector(action)]autorelease];  
          6. get1Button.width = 50;  
          7. [myToolBarItems addObject:myButton];      
           

          C>加一個文本Label

          1. UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40.0f, 20.0f, 45.0f, 10.0f)];  
          2. myLabel.font=[UIFont systemFontOfSize:10];  
          3. //myLabel.backgroundColor = [UIColor clearColor];  
          4. //myLabel.textAlignment=UITextAlignmentCenter;  
          5. UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc]initWithCustomView:myLabel];  
          6. [myToolBarItems addObject: myButtonItem];     
          7. [mylabel release];  
          8. [myButtonItem release];  
           

           

           

          D>加一個進度條UIProgressView

           

          1. UIProgressView *myProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(65.0f, 20.0f, 90.0f, 10.0f)];  
          2. UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc]initWithCustomView:myProgress];  
          3. [myToolBarItems addObject: myButtonItem];  
          4. [myProgress release];                                             
          5. [myButtonItem release];  
           

          可以加使用initWithCustomView制作各種button,這里就不在這里一個一個在加了。我想你應該也已經掌握了如何添加各種buttonItem的方法了。

          posted on 2011-08-16 10:09 棋劍小秋 閱讀(523) 評論(0)  編輯  收藏 所屬分類: iPhone
          主站蜘蛛池模板: 靖安县| 平舆县| 迁西县| 陆川县| 中山市| 紫云| 云梦县| 南华县| 连云港市| 凤山市| 南阳市| 吴堡县| 望都县| 东城区| 敖汉旗| 鹤峰县| 泾川县| 乡城县| 浦北县| 老河口市| 大关县| 威宁| 水城县| 沂源县| 翁牛特旗| 贵德县| 安远县| 延安市| 义乌市| 临夏市| 登封市| 新沂市| 八宿县| 修水县| 白朗县| 苗栗市| 宁安市| 吉安县| 凤庆县| 宝兴县| 大理市|