Delegate method not being called objective C

2019-07-09 23:18发布

I am trying yo pass data of two textfields in secondViewController to ViewController and set text of labels in ViewController.

But the delegate method for passing data is not being called. I have checked it by putting break point. Hence label text is not changing.

SecondViewController.h

#import <UIKit/UIKit.h>

@class SecondViewController;
@protocol SecondViewDelegate <NSObject>

-(void)getText1:(NSString*)str1 andText2:(NSString*)str2;

@end
@interface SecondViewController : UIViewController<UITextFieldDelegate>


@property (weak, nonatomic) IBOutlet UITextField *textField1;
@property (weak, nonatomic) IBOutlet UITextField *textField2;
@property (weak) id<SecondViewDelegate>delegate;

@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()




@end

@implementation SecondViewController

@synthesize delegate=_delegate;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textField1.delegate=self;
    self.textField2.delegate=self;
    [self.textField1 becomeFirstResponder];

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField
{


    if ( textField == self.textField1 ) { [self.textField1 resignFirstResponder]; [self.textField2 becomeFirstResponder]; }
    else if ( textField == self.textField2)  {


        [_delegate getText1:self.textField1.text andText2:self.textField2.text];

        NSLog(@"%@",self.textField1.text);
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    return YES;
}
@end

View Controller.h

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

@interface ViewController : UIViewController<SecondViewDelegate>


-(void)getText1:(NSString *)str1 andText2:(NSString *)str2;
@end

View Controller.m

#import "ViewController.h"


@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;


@end

@implementation ViewController
@synthesize label1;
@synthesize label2;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)onClick:(id)sender {
    SecondViewController* sv= [[SecondViewController alloc] init];
    sv.delegate=self;

    [self performSegueWithIdentifier:@"moveToSecondController" sender:self];
}

-(void)getText1:(NSString *)str1 andText2:(NSString *)str2{

    [label1 setText:str1];
    [label2 setText:str2];


}

@end

4条回答
该账号已被封号
2楼-- · 2019-07-10 00:05

Try setting your delegate in prepareForSegue method like:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        SecondViewController *vc = [segue destinationViewController];
        vc.delegate=self;

    }
}
查看更多
Melony?
3楼-- · 2019-07-10 00:08

import

import "SecondViewController.h"

@interface ViewController : UIViewController

// Remove this method in ViewController.h file this is call as a simple method for ViewController class -(void)getText1:(NSString *)str1 andText2:(NSString *)str2;

@end

查看更多
Ridiculous、
4楼-- · 2019-07-10 00:11

The problem is that you've created two SecondViewController objects and made your ViewController the delegate of the wrong one.

This: [[SecondViewController alloc] init] creates an object in code. This: [self performSegueWithIdentifier:@"moveToSecondController" sender:self] creates an object from a storyboard definition.

Don't bother creating the first one, just perform the segue. Then, implement the prepareForSegue method and set your delegate there, using the destination controller (which will be the correct SecondViewController).

查看更多
手持菜刀,她持情操
5楼-- · 2019-07-10 00:15

You don't need to declare your delegate method in ViewController.h. It has already been done in SecondViewController.h as the delegate method.

查看更多
登录 后发表回答