2011年9月10日 星期六

Lala's Program Note 實作記錄: 17. 調整 NoteBook 刪除與新增的按鈕

since: 2011/09/10
update: 2011/09/10


A. 說明: 主要是讓操作能有一致性.


B. 作法:
   1. 在 NoteBookViewController.h 宣告三個 method.
// 用來產生 segmentBarItem
- (UIBarButtonItem *)segmentBarItemMakeWithSelector:(SEL)aSelector btn1Name:(NSString *)btn1Name btn2Name:(NSString *)btn2Name;

// 切換刪除狀態與功能
- (void)toggleDelete;

// 按下 segment 要作的事
- (void)segmentAction:(id)sender;

-------------------------------------------------------------------------------------------------------

2. 在 NoteBookViewController.m 定義此三個 method 的內容.
// 用來產生 segmentBarItem
- (UIBarButtonItem *)segmentBarItemMakeWithSelector:(SEL)aSelector btn1Name:(NSString *)btn1Name btn2Name:(NSString *)btn2Name {
   
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                              [NSArray arrayWithObjects:

                                             //[UIImage imageNamed:@"itrash-50.png"],
                                             //@"Trash",
                                             btn1Name,
                                             btn2Name,
                                             nil]];
   
    [segmentedControl addTarget:self action:aSelector forControlEvents:UIControlEventValueChanged];

    segmentedControl.frame = CGRectMake(0, 0, 90, 35);
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    segmentedControl.momentary = YES;
   
    UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];

    [segmentedControl release];
   
    return [segmentBarItem autorelease];
}

-------------------------------------------------------------------------------------------------------

// 切換刪除狀態與功能
- (void)toggleDelete {
    /* 切換編輯(刪除)狀態 */
    [self setEditing:!self.editing animated:YES];
   
    /* 設定 selector */
    SEL segmentAction = @selector(segmentAction:);

    /* 正在編輯(刪除)中 */
    if (self.editing) {
        self.navigationItem.rightBarButtonItem = [self segmentBarItemMakeWithSelector:segmentAction btn1Name:@"Done" btn2Name:@"Add"];
    }
    /* 尚未編輯(刪除) */
    else {
        self.navigationItem.rightBarButtonItem = [self segmentBarItemMakeWithSelector:segmentAction btn1Name:@"Trash" btn2Name:@"Add"];
    }
}

-------------------------------------------------------------------------------------------------------

// 按下 segment 要作的事
- (void)segmentAction:(id)sender{
   
    if([sender selectedSegmentIndex] == 0){
        //do something with segment 1
        // Trash
        [self toggleDelete];
       
    }else if([sender selectedSegmentIndex] == 1){
        //do something with segment 2
        // Add
        [self showManageNBView];
    }
}

-------------------------------------------------------------------------------------------------------

3. 修改 NoteBookViewController.mviewDidLoad method:
- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; // 實際上是進入刪除的功能
    //@add
    //@deleteButton
    /*
    UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(toggleDelete)];
    self.navigationItem.leftBarButtonItem = deleteButton;
    [deleteButton release];
    */
   
    //@addButton
    /*
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showManageNBView)];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];
     */
   
    //@add
    SEL segmentAction = @selector(segmentAction:);
   
    self.navigationItem.rightBarButtonItem = [self segmentBarItemMakeWithSelector:segmentAction btn1Name:@"Trash" btn2Name:@"Add"];
}

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。