委托方法不被称为目标C [英] Delegate method not being called objective C

查看:80
本文介绍了委托方法不被称为目标C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将secondViewController中的两个文本域的数据传递给ViewController,并在ViewController中设置标签的文本。

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

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

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

查看Controller.h

View Controller.h

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

@interface ViewController : UIViewController<SecondViewDelegate>


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

查看Controller.m

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


推荐答案

问题是您已经创建了两个 SecondViewController 对象,并使您的 ViewController 代理错误的一个。

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

这样: [[SecondViewController alloc] init] 创建一个对象在代码中。这个: [self performSegueWithIdentifier:@moveToSecondControllersender:self] 从故事板定义创建一个对象。

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

不要打扰创建第一个,只是执行segue。然后,使用目标控制器(这将是正确的 SecondViewController )实现 prepareForSegue 方法并设置您的委托, 。

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).

这篇关于委托方法不被称为目标C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆