0

I have a TableView which is HomeViewController I also have another View which is NewViewController

In HomeViewController I can add items to the plist such as "Book Title" key by doing:

NSString *bookTitle;

self.books addObject:bookTitle

then a button is clicked to push to NewViewController where there is a text field. When the save button is clicked I need the text for the textField to be added to the table view "Book text" key in the plist

But I cannot do self.books in this class so how can I add to the tableview thats in HomeViewController from NewViewController

1
  • Can you paste your code what you have tried so far?? Commented Nov 17, 2013 at 13:52

1 Answer 1

1

Define a delegate method in NewViewController that is called when the button is clicked, passing the textField content to the HomeViewController. The HomeViewController implements the delegate method and can then update the books collection as appropriate. E.g.:

NewViewController.h

// Define the delegate method(s)
@protocol NewViewControllerDelegate <NSObject>

-(void)addedTitle:(NSString *)text;

@end

@interface NewViewController : UIViewController

// Declare the delegate property
@property (nonatomic, weak)id <NewViewControllerDelegate>delegate;

// other declarations...

@end

NewViewController.m

#import "NewViewController.h"

@interface NewViewController ()

@end

@implementation NewViewController

// other implementation code...

-(IBAction)buttonTapped:(id)sender{
    [self.delegate addedTitle:textField.text];
}

@end

HomeViewController.h

#import <UIKit/UIKit.h>
#import "NewViewController.h"

// Note <NewViewControllerDelegate> on next line
@interface HomeViewController : UIViewController <NewViewControllerDelegate>

// other declaration code...

@end

HomeViewController.m

#import "HomeViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

-(void)viewDidLoad{
    [super viewDidLoad];

    myNewViewController.delegate = self;
}

// other implementation code...

#pragma mark - NewViewController delegate methods
// Implements the delegate callback
-(void)addedTitle:(NSString *)title{
    [self.books addObject:title];
}

@end
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, I am quite new to this, can you show some code or anything to aid me, thanks.
Thanks alot! ill have a crack at it now
Just come accross a problem actually, in the HomeViewController.m "title" contains nothing when I NSLog it
If you NSLog textView.text in the buttonTapped method in NewViewController, what do you get?
i think its due to the fact that self.delegate is null
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.