All*_*nDo 6 objective-c uitableview ios uistoryboard
我无法连接数据源并将故事板中的表视图的出口委托给我的自定义委托类.我想将这些表函数委托给另一个类.在故事板中,有一些我从根本上误解了关于委托,出口和布线的事情.
背景
我有一个UIViewController
包含a UIPickerView
等等的视图UITableView
.
我已经达到了我UIViewController
太大的程度,我想将与表相关的函数移到另一个类中.
我创建了以下类来包含那些表方法,如numberOfSectionsInTableView:
.
@interface ExerciseTableDelegate : NSObject <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) ExerciseDataController *dataController;
@end
Run Code Online (Sandbox Code Playgroud)
我曾想过在我的课堂上提到上面的课程 UIViewController
@interface ExerciseViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
{
UIPickerView *exercisePicker;
}
@property (strong, nonatomic) IBOutlet ExerciseTableDelegate *tableDelegate;
@end
Run Code Online (Sandbox Code Playgroud)
我希望在故事板中,当我将表视图中的一个数据源或委托出口拖到UITableViewController
它上面时,它将使我能够连接到我的委托类.它没有.
然后我尝试在故事板中创建一个对象,为它提供类ExerciseTableDelegate
.然后我可以将表视图委托拖动到对象,但这不是我在我设置的对象AppDelegate
.
我的应用代表
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
ExerciseViewController *rootViewController = (ExerciseViewController *)[[navigationController viewControllers] objectAtIndex:0];
ExerciseTableDelegate *tableDelegate = [[ExerciseTableDelegate alloc]init];
ExerciseDataController *dataController = [[ExerciseDataController alloc] init];
tableDelegate.dataController = dataController;
rootViewController.tableDelegate = tableDelegate;
// Override point for customization after application launch.
return YES;
}
Run Code Online (Sandbox Code Playgroud)
我觉得我很亲密,但我觉得我做得太多了.
如果您想访问在应用程序委托中设置的实例ExerciseTableDelegate
,那么您必须在代码中将其连接到表视图,因为无法从故事板访问它 - 正如您所发现的,添加故事板中的新对象创建一个新实例。
幸运的是,这实现起来非常简单。在viewDidLoad
表视图控制器的方法中,添加以下内容:
self.tableView.delegate = self.tableDelegate;
self.tableView.datasource = self.tableDelegate;
Run Code Online (Sandbox Code Playgroud)
这将重新指向数据源并委托给您单独的对象。
归档时间: |
|
查看次数: |
3976 次 |
最近记录: |