0

i am new to ios programming; I write an app to show a recipe list in a UITableView and after select a row, row title parse to a UITextField in detail view.

question: what can i do for save title change in detailview to table view (i use storyboard)?

this is my code :

NViewController.m :

- (void)viewDidLoad
{
    [super viewDidLoad];

    recipes = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", nil];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"RecipeCell";
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell.lblName.text = [recipes objectAtIndex:indexPath.row];
    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
        NSIndexPath *indexPath = [self._tableView indexPathForSelectedRow];
        NDetailViewController *destViewController = segue.destinationViewController;
        destViewController.selectedRowTitle = [recipes objectAtIndex:indexPath.row];
    }
}

NDetailViewController.m :

@interface NDetailViewController ()
@property (weak, nonatomic) IBOutlet UITextField *txtDetailText;

@end

@implementation NDetailViewController

@synthesize selectedRowTitle;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.txtDetailText.text = selectedRowTitle;
}
nmokkary
  • 1,219
  • 3
  • 14
  • 24
  • If your "recipe" is an object, maybe you can pass that object to the `NDetailViewController` when it is presented. Then, when you're about to dismiss the detail view controller, you set the recipe object's name property (or whatever you have) to the text of the `UITextField`. –  Apr 26 '14 at 11:50
  • Please explain you question. Are you trying to pass data(a string in your case) to details view? Also what viewcontroller are you using? Navigation controller ? Or splitviewcontroller. – BangOperator Apr 26 '14 at 11:54
  • no i want pass data from detail view to master view and change table view content? – nmokkary Apr 26 '14 at 11:56

2 Answers2

1

In NViewController,you add NSIndexPath to save:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
    NSIndexPath *indexPath = [self._tableView indexPathForSelectedRow];
    NDetailViewController *destViewController = segue.destinationViewController;
    destViewController.selectedRowTitle = [recipes objectAtIndex:indexPath.row];

    // need to save this indexPath to update when textfield is changed. Because indexPath maybe change if data changes, I think you should choose another field (like KEY, distingue objects)
    destViewController.indexPath = indexPath;
    destViewController.delegate = self;
}
}

-(void)changeTextFieldWithIndexPath : (NSIndexPath*)indexPath content:(NSString*)content;
{
 // you update indexPath to row, and reloadTable
}

NDetailViewController.h:

@protocol NDetailViewControllerDelegate <NSObject>
-(void)changeTextFieldWithIndexPath : (NSIndexPath*)indexPath content:(NSString*)content;
@end

@interface NDetailViewController : ViewController
@property (nonatomic, weak) id<NDetailViewControllerDelegate> delegate;
@end

NDetailViewController.m: When textfiled changes value, my method is just a example:

- (void) whenContentOfTextFieldChanged
{
[self.delegate changeTextFieldWithIndexPath:indexPath content:@"content which textfield is changed"];
}
nmh
  • 2,497
  • 1
  • 15
  • 27
  • these lines have error : destViewController.indexPath = indexPath; destViewController.delegate = self; – nmokkary Apr 26 '14 at 12:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51492/discussion-between-nmh-and-nmokkary) – nmh Apr 26 '14 at 13:15
0

Assuming you are using UISplitview controller , you can access the masterview by [self.splitviewcontroller.viewcontrollers objectatindex:0] and call defined method upon it.

Also you can use notification to pass data Check this answer for notification sample: How to pass object with NSNotificationCenter

Community
  • 1
  • 1
BangOperator
  • 4,377
  • 2
  • 24
  • 38