ios - 委托(delegate)方法未被称为 Objective-C

标签 ios objective-c delegates

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

但是没有调用传递数据的委托(delegate)方法。我已经通过放置断点来检查它。因此标签文本没有改变。

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

查看 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

最佳答案

问题是您创建了两个 SecondViewController 对象并使您的 ViewController 成为错误对象的委托(delegate)。

This: [[SecondViewController alloc] init] 在代码中创建一个对象。这:[self performSegueWithIdentifier:@"moveToSecondController"sender:self] 从 Storyboard定义创建一个对象。

不要费心创建第一个,只需执行 segue 即可。然后,使用目标 Controller (将是正确的 SecondViewController)实现 prepareForSegue 方法并在那里设置您的委托(delegate)。

关于ios - 委托(delegate)方法未被称为 Objective-C ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36154531/

相关文章:

objective-c - 写入驻留在 Xcode 项目目录中的文件

objective-c - 我该如何解决错误?

c# - 委托(delegate)指向具有不同签名的多个函数

ios - Google Plus 使用 UIWebView 登录 iOS

objective-c - 将 vCard 数据直接添加到系统地址簿

ios - 自动编辑 NSString?

ios - 委托(delegate)是否仍然不安全未保留?

c# - 如何为具有空目标的实例方法创建委托(delegate)?

ios - 使用自定义转换时, View Controller 是否会被完全删除?

ios - (反射(reflection))在 Swift 中按函数名调用带参数的方法