2012-04-22 35 views
0

我創建了一個自定義TablePickerViewController這是的子類UITableViewController。我使用這個類來顯示一個自定義類型的對象列表TablePickerItem如何生成通用表格視圖控制器?

我使用TablePickerViewController多次在我的iOS應用程序,以顯示不同類型的列表,其中用戶必須選擇一個項目 - 然後另一個視圖控制器MainViewController應該在此選擇反應,並做一些事情。

我創造了這個協議,並在TablePickerViewController創建一個代表屬性:

@protocol TablePickerViewControllerDelegate <NSObject> 
- (void)tablePickerViewController:(TablePickerViewController *)controller 
        didSelectItem:(TablePickerItem*)item; 
@end 

當我安裝一個新的TablePickerViewControllerMainViewController它也被設置爲代理 - 比將在用戶點擊表格視圖中的單元時通知。

的問題是,我MainViewController將設置多個TablePickerViewController不同的數據(TablePickerItem)。我應該如何設置我的MainViewController來處理這些多重TablePickerViewController?來自它們每個的事件將導致調用我的MainViewController中的相同協議方法。

此外,我需要獲取TablePickerItem表示的元素,因爲我需要知道元素ID在tablePickerViewController:didSelectItem方法中的作用。我是否應該通過向TablePickerItem添加類似@property (nonatomic) id element的東西來處理此問題,並將原始對象設置爲此屬性,然後創建它?

也許有人可以給我一個關於如何創建一個通用表視圖控制器的例子,如果我的解決方案似乎以錯誤的方式完成。

+0

你能分享這門課嗎?我有同樣的需要,如果可以發送到[email protected] – Nepster 2014-09-24 10:04:25

回答

0

我並不完全確定你的設置,但是如果你有多個選擇器反饋給主控制器,那麼你可以只提供選擇器的引用,例如,

// MainViewController.m 

@interface MainViewController() 

@property (nonatomic, strong) TablePickerViewController *picker1; 
@property (nonatomic, strong) TablePickerViewController *picker2; 
// ... and so on. Obviously you know your problem domain so you can change 
// the terrible naming above to something appropriate 

@end 

@implementation MainViewController 


// ... 

- (void)theMethodWhereYouSetUpYourPickers; 
{ 
    TablePickerViewController *picker1 = [[TablePickerViewController alloc] init]; 
    picker1.delegate = self; 
    self.picker1 = picker1; 
    // ... 
} 

- (void)tablePickerViewController:(TablePickerViewController *)controller 
       didSelectItem:(TablePickerItem*)item; 
{ 
    if (controller == self.picker1) { 
     NSLog(@"Something was picked in picker 1 %@", item); 
    } else if (controller == self.picker2) { 
     NSLog(@"Something was picked in picker 2 %@", item); 
    } 
} 

// ... 

@end 
相關問題