2012年9月25日 星期二

UIToolBar的基本使用(ㄧ)


一個最基本的ToolBar的使用,並聯合一個TableView來做Menu的顯示
Xcode版本4.5 & IOS6


1. 開一個基本的專案






2. 加入一個新的class 檔給TableView使用




3. 設定View為Landscape,並加入一個Toolbar



4. 加入一個NavigationBar

5. 對NavigationBar加入一個BarItem,並將Item命名為Menu


6. 對ToolBar加入一個BarItem,並將Item命名為ToolMenu

7. 加入一個UITableView Controller

8.對兩個所增加的BarItem,各拉一個Segue到TableView Controller,並設定為popover

9. 將TableView Controller設定給tableViewController.h /m檔案

10. 對tableViewController.h,加入Table變數
#import <UIKit/UIKit.h>

@interface tableViewController : UITableViewController

@property (retain)  NSArray *myData;

@end

11.對tableViewController.m,加入所需程式碼
#import "tableViewController.h"

@interface tableViewController ()

@end

@implementation tableViewController

@synthesize myData;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    myData = [[NSArray alloc] initWithObjects:@"First",@"Second",@"Third",@"Fourth",@"Fifth", nil];
   
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    //return 0;

    return [myData count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
   
    cell.textLabel.text = (NSString *)[myData objectAtIndex:indexPath.row];
   
    return cell;
}

11. 執行結果,因為兩個BarItem都指向同一個TableView,因此結果都顯示相同的TableView,但是所指向的啓動位置,會不一樣。


沒有留言: