-2

I want to transfer data from view controller to second view controller using delegate. What mistake I am doing, why my protocol is not confirming on second view controller. Code:

"This Is my view Controller code"

ViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"

@protocol ViewControllerProtocol <NSObject>
-(void)passData:(NSString*)data;
@end

@interface ViewController : UIViewController

@property id<ViewControllerProtocol>delegateVC;
@property (weak, nonatomic) IBOutlet UITextField *txtFieldVC;
- (IBAction)btnSendVC:(id)sender;
@end


ViewController.m
#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)btnSendVC:(id)sender {
    [self.delegateVC passData:self.txtFieldVC.text];
    [self performSegueWithIdentifier:@"next" sender:self];
}
@end//

"This is my second view Controller code"
SecondViewController.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface SecondViewController : UIViewController<ViewControllerProtocol>

- (IBAction)btnSVC:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *txtFieldSVC;
@end

//  SecondViewController.m

#import "SecondViewController.h"
@interface SecondViewController ()
@end

@implementation SecondViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)passData:(NSString *)data {
    self.txtFieldSVC.text = [NSString stringWithFormat:@"%@",data];
    NSLog(@"Data Received: %@",data);
}
- (IBAction)btnSVC:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end

Where Iam wrong is that I am missing something...

raj
  • 93
  • 1
  • 9
  • 4
    you should write code in question instead of uploading screenshot!!! – Ketan Parmar Oct 27 '16 at 08:29
  • Image of second viewcontroller missing instead post the code directly – Vinodh Oct 27 '16 at 08:32
  • You don't need a delegate; you can use `prepareForSegue` but regardless, the way you are doing it won't work since you are trying to access the delegate before the second view controller has been instantiated – Paulw11 Oct 27 '16 at 08:37
  • As I said, you can't use a delegate to pass data "forward". – Paulw11 Oct 27 '16 at 19:39

1 Answers1

1

If we use above code,it is not possible to send the data from First View Controller to Second View Controller using Custom Delegate.But if we want to send data from Second View Controller to First View Controller,it is possible.If you want to send the data from Next View Controller to Previous View Controller you can do it using Custom Delegate and Notification methods.

Now your requirement is you have to send the data from First View Controller to Second View or Next View Controller.For that you can use prepareForSegue and performSegueWithIdentifier method for storyboard.It is enough.

user3182143
  • 9,459
  • 3
  • 32
  • 39