3

I created master details template project in xcode 4.6 and I added custom cell with 2 textfields. I also created new class which is subclass of UITableViewCell and inside this class I created outlets for text fields. When user types something NSMutableArray is updated and this works fine. Now I am wondering how to pass this array back to MasterViewController (UITableViewController) so that I can use this data to show calculations.

I tried using tutorials for delegates between UIViewControllers but I keep getting errors. Any help is appreciated.

iPatel
  • 46,010
  • 16
  • 115
  • 137
James Douglas
  • 736
  • 1
  • 11
  • 20
  • Inside your cell subclass add a field to contain a pointer back to the controller or whatever other data anchors you need to access. In your case you'd probably make a call back to the master VC to communicate the update. – Hot Licks Apr 01 '13 at 20:39

2 Answers2

2

You shouldn't keep data inside the UITableViewCell, as it breaks the MVC.

You need to get a reference of the UITextField on your cell. This is how I do in a login form:

I have a custom cell subclass called TextFieldCell, it has an outlet called textField, I want that my UITableViewController have references to these UITextFields.

First I open my storyboard, set the cell class to TextFieldCell and than connect the UITextField to cell textField outlet. Than I add this to the 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;
    (…)
}

Now I can access the value of my loginTextField and my passwordTextField. I do that on the tableView:cellForRowAtIndexPath: because that's when I'm creating the cell to add to the table view.

Marco Pompei
  • 1,080
  • 1
  • 8
  • 18
  • But how can I create outlet for the cell when I get error saying connection textField cannot have prototype object as its destination – James Douglas Apr 01 '13 at 22:00
  • You can't directly connect the Cell to the View Controller as the cell isn't a real object, it's a prototype to initialize objects. You need to create a custom `UITableViewCell` subclass with the outlets you need and connect the `UITextField` **to the cell**. – Marco Pompei Apr 02 '13 at 12:02
  • I did that I created new class which is subclass of UITableViewCell and it works fine. However I am stuck with how to pass this data to other class like UITableViewController? – James Douglas Apr 02 '13 at 12:04
  • You do that where your UITableViewController have a reference to the UITableViewCell, I do that on my `tableView:cellForRowAtIndexPath:`, there I get variables pointing to the text fields, but you could simply get a variable pointing to the cell and than accessing data structures inside that cell. Just be aware the storing and specially processing data inside a View is usually a Bad Thing. – Marco Pompei Apr 02 '13 at 12:15
0

In your case you need to create Protocol:

I just Give Basic Idea for how to Create Protocol

Also Read This Question

#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);

}
Community
  • 1
  • 1
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • You are doing protocol by pushing viewController while in my case this line of code is useless as I send data from the class which is not ViewController: [self.navigationController pushViewController:enterAmountVC animated:YES]; – James Douglas Apr 02 '13 at 11:00