一步一步熟悉Mac app開發(fā)(十)之Cocoa Binding

概要

階段一、實現(xiàn)數(shù)據(jù)的在table view上的顯示、增加。(復習ArrayController)
階段二、通過使用tableview的委托方法tableViewSelectionDidChange來實現(xiàn)將選中項的值賦給文本框。
階段三、通過使用文本框的委托方法controlTextDidChange實現(xiàn)table view內數(shù)據(jù)的動態(tài)修改。

階段一 數(shù)據(jù)的顯示與增加

1、新建工程,打開StoryBoard,添加Push Button、Text Filed、Label、Array Controller、Table View等。

image.png

2、創(chuàng)建一個NSObject的子類User,并為其添加firstName與lastName成員。

//User.h
#import <Foundation/Foundation.h>
@interface User : NSObject
@property NSString *firstName;
@property NSString *lastName;
-(instancetype) init;
@end
//User.m
#import "User.h"
@implementation User
-(instancetype) init{
    self = [super init];
    if(self){
        self.firstName = @"Gao";
        self.lastName = @"Ben";
    }
    return self;
}
@end

3、向ViewController.h中添加NSMutableArray類型的成員變量users并在ViewController.m中對其進行初始化。

//ViewController.h
#import <Cocoa/Cocoa.h>
#import "User.h"
@interface ViewController : NSViewController
@property NSMutableArray<User*> *users;
@end
//ViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.users = [[NSMutableArray alloc] initWithCapacity:32];
}

4、打開StoryBoard,設置Array Controller的Object Controller中的Class Name為User,將Array Controller的Content Array與ViewController的users進行綁定。

image.png
image.png

5、將Table view的Table Content與Array Controller進行綁定,并將Add按鈕與Array Controller的Add方法進行綁定。

image.png
image.png
image.png
image.png

6、階段一完成,效果如下。

image.png

階段二 選中項處理

1、將table view與兩個text filed拖拽大法至ViewController.h中,并添加NSTableViewDelegate協(xié)議,拖拽細節(jié)省略,代碼如下。

#import "ViewController.h"
#import "User.h"
@interface ViewController()<NSTableViewDelegate>
@property (weak) IBOutlet NSTableView *tableView;
@property (weak) IBOutlet NSTextField *text_firstName;
@property (weak) IBOutlet NSTextField *text_lastName;
@property  User *currentUser;
@end

2、設置table view的delegate為View Controller,并實現(xiàn)其委托協(xié)議方法。

image.png
- (void)tableViewSelectionDidChange:(NSNotification *)notification{
    if(self.tableView.selectedRow < 0){
        self.text_firstName.stringValue = @"";
        self.text_firstName.stringValue = @"";
        return ;
    }
    self.currentUser = self.users[self.tableView.selectedRow];
    self.text_firstName.stringValue = self.currentUser.firstName;
    self.text_lastName.stringValue = self.currentUser.lastName;
}

3、完成,效果如下。

image.png

階段三 數(shù)據(jù)的動態(tài)修改

1、新增“Update”按鈕,拖拽大法至ViewController.m內,命名為btn_update。


image.png

2、將表示First Name的文本框與currentUser.firstName綁定起來,將表示Last Name的文本框與currentUser.lastName綁定起來。

image.png
image.png

3、將First Name文本框、Last Name文本框委托給View Controller。

image.png
image.png

4、在ViewController.m中添加臨時委托協(xié)議NStextFiledDelegate,并實現(xiàn)controlTextDidChange方法。

#import "ViewController.h"
#import "User.h"
@interface ViewController()<NSTableViewDelegate,NSTextFieldDelegate>
@property (weak) IBOutlet NSTableView *tableView;
@property (weak) IBOutlet NSTextField *text_firstName;
@property (weak) IBOutlet NSTextField *text_lastName;
@property  User *currentUser;
@end

@implementation ViewController
//new
- (void)controlTextDidChange:(NSNotification *)obj{
    self.currentUser.firstName = _text_firstName.stringValue;
    self.currentUser.lastName = _text_lastName.stringValue;
}

- (IBAction)btn_update:(id)sender {
    //多余的更新按鈕
    NSLog(@"%@ %@",self.currentUser.firstName,self.currentUser.lastName);
}

- (void)tableViewSelectionDidChange:(NSNotification *)notification{
    if(self.tableView.selectedRow < 0){
        self.text_firstName.stringValue = @"";
        self.text_firstName.stringValue = @"";
        return ;
    }
    self.currentUser = self.users[self.tableView.selectedRow];
    //self.text_firstName.stringValue = self.currentUser.firstName;
    //self.text_lastName.stringValue = self.currentUser.lastName;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //4C3TJ-8XH4K-MPQCM-R6C4X-P3C9D
    // Do any additional setup after loading the view.
    self.users = [[NSMutableArray alloc] initWithCapacity:32];
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];
    // Update the view, if already loaded.
}
@end

4、完成,效果如下。

image.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,186評論 3 119
  • 1.ios高性能編程 (1).內層 最小的內層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結構(3).初始化時...
    歐辰_OSR閱讀 30,214評論 8 265
  • 2015年1月的舊文,寫得特別虛弱【不愿意用矯情來形容自己,(⊙v⊙)】,現(xiàn)在想起來,的確是花太多時間在最好的朋友...
    微冷微冷閱讀 2,939評論 5 7
  • 昨日寫作5321字?!豆适鹿8?5-新年禮物》。 寫了五個故事梗概,連同之前的《玩偶》,一共是六篇文章備選。想了一...
    朝朝暮暮237閱讀 150評論 0 1

友情鏈接更多精彩內容