bardockyo
bardockyo

Reputation: 1435

getting value from a textfield inside a cell in xcode

So I have a popup table view that displays 3 names. What I am trying to get is when a user selects a name from the popup table it will close the popup and display that name on a label or textfield on the original view controller. Here is what I have so far:

(selectedName is the name of the label I have on the original viewcontroller)

- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    selectedName.text = cell.textLabel.text;



}

Its not working and I am also looking for a way for the popup to disappear when a cell is clicked. Any ideas?

EDIT: (code for setting cell label text)

cell.textLabel.text = [[myArray objectAtIndex:indexPath.row] objectForKey:@"Name"];

Upvotes: 1

Views: 1921

Answers (1)

Jeremy
Jeremy

Reputation: 9030

What you can do is to define a protocol that your tableview can use to communicate to the presenter. This is a common practice.

Here is a crude example:

@protocol MyTableViewDelegate<NSObject>
-(void)myTableView:(TheTableViewClass *)tableView didSelectValue:(NSString *)value;
@end

@interface TheTableViewClass : UITableViewController
@property (nonatomic, assign) id<MyTableViewDelegate> d;
@end


@implementation TheTableViewClass
@synthesize d;
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [self.d myTableView:self didSelectValue:cell.textLabel.text];
}
@end


@implementation MyPresenter
-(void)present
{
    self.myTableViewClass = [MyTableViewClass ....];
    self.myTableViewClass.d = self;
    //present myTableViewClass...
}
-(void)myTableView:(TheTableViewClass *)tableView didSelectValue:(NSString *)value
{
    //Set some label to the value passed in
    //Dismiss self.myTablViewClass
}
@end

Some additional reading: Working with Protocols

Upvotes: 1

Related Questions