2011年9月10日 星期六

Lala's Program Note 實作記錄: 16. 調整文章列表刪除與新增的按鈕

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


A. 說明:
   原本的刪除功能是在 EditDone 之間切換, 易讓使用者混淆, 並且會讓
   navigation bar回上頁消失,
因此改成以 segmentedControl 的方式與
   新增功能放在一起.



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

      // 切換刪除狀態與功能

       - (void)toggleDelete;

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

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

   2. 在 ArticleListViewController.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 showManageArticleView];
    }
}

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

3. 修改 ArticleListViewController.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 編輯按鈕 (實際上是進入刪除的功能)
    // 在此處, 會讓 navigation bar 的回上頁消失 (之後再處理 "刪除" 功能)
    //self.navigationItem.leftBarButtonItem = self.editButtonItem;
   
    //@deleteButton
    /*
    UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(toggleDelete)];
   
    self.navigationItem.leftBarButtonItem = deleteButton;
    [deleteButton release];
    */
   
    //@add 新增按鈕
    /*
     UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showManageArticleView)];
    
     self.navigationItem.rightBarButtonItem = addButton;
     [addButton release];
     */  
   
    //@add
    SEL segmentAction = @selector(segmentAction:);


    self.navigationItem.rightBarButtonItem = [self segmentBarItemMakeWithSelector:segmentAction btn1Name:@"Trash" btn2Name:@"Add"];
}

沒有留言:

張貼留言

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