在UITableViewCell和UITableViewController之间传递数据?
我在xcode 4.6中创build了主细节模板项目,并添加了2个文本框的自定义单元格。 我还创build了一个新的类,它是UITableViewCell的子类,在这个类中我创build了文本字段的sockets。 当用户键入的东西NSMutableArray更新,这工作正常。 现在我想知道如何将这个数组传递给MasterViewController(UITableViewController),以便我可以使用这些数据来显示计算。
我尝试使用UIViewControllers之间的委托教程,但我不断收到错误。 任何帮助表示赞赏。
你不应该把数据保存在UITableViewCell
,因为它打破了MVC。
您需要获取单元格上的UITextField
的引用。 这是我如何在login表单中做的:
我有一个名为TextFieldCell
的自定义单元格子类,它有一个名为textField
的出口,我希望我的UITableViewController
具有对这些UITextField
的引用。
首先,我打开故事板,将单元格类设置为TextFieldCell,然后将UITextField连接到单元格textFieldsockets。 比我添加到tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { (…) if (indexPath.row == 0) { // Sets the textField of the first cell as the loginTextField. self.loginTextField = tCell.textField; } else { // Sets the textField of the second cell as the passwordTextField. self.passwordTextField = tCell.textField; } tCell.textField.delegate = self; (…) }
现在我可以访问我的loginTextField
和我的passwordTextField
的值。 我这样做tableView:cellForRowAtIndexPath:
因为这是当我创build单元格添加到表视图。
在你的情况下,你需要创build协议:
我只是给如何创build协议的基本思路
也读这个问题
#DetailViewController.h #import <UIKit/UIKit.h> @protocol MasterDelegate <NSObject> -(void) getButtonTitile:(NSString *)btnTitle; @end @interface DetailViewController : MasterViewController @property (nonatomic, assign) id<MasterDelegate> customDelegate; #DetailViewController.m if([self.customDelegate respondsToSelector:@selector(getButtonTitile:)]) { [self.customDelegate getButtonTitile:button.currentTitle]; } #MasterViewController.m create obj of DetailViewController DetailViewController *obj = [[DetailViewController alloc] init]; obj.customDelegate = self; [self.navigationController pushViewController:reportTypeVC animated:YES]; and add delegate method in MasterViewController.m for get button title. #pragma mark - #pragma mark - Custom Delegate Method -(void) getButtonTitile:(NSString *)btnTitle; { NSLog(@"%@", btnTitle); }
- 为什么我的UITabBar在纵向加载时向下移动20个像素?
- 将NSString转换为date
- 我怎样才能以编程方式设置选定的UITabBarController选项卡,同时也触发UITabBarControllerDelegate中的shouldSelectViewController
- 在运行时更改iOS模拟器的当前区域设置
- parsingNSURLpath和查询(iphoneOS)
- 书法绘图IOS Coregraphics
- 为什么使用HTMLstring对NSAttributedString的初始调用比后续调用要长100倍?
- 如何使用iOS Simulator 7 \ XCode 5来testingiOS 7之前的UI(无需升级iOS 7的应用程序)
- 应用程序审查链接不工作了