委托方法不被称为客观的C

我想哟传递数据的两个textfields在secondViewController到ViewController和在ViewController中设置标签的文本。

但是传递数据的委托方法没有被调用。 我已经通过把断点检查它。 因此标签文本不会改变。

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 

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

问题是你已经创build了两个SecondViewController对象,并使你的ViewController成为错误的委托。

这: [[SecondViewController alloc] init]在代码中创build一个对象。 这: [self performSegueWithIdentifier:@"moveToSecondController" sender:self]从故事板定义中创build一个对象。

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

尝试在prepareForSegue方法中设置您的委托,如:

 - (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; } } 

你不需要在ViewController.h声明你的delegate method 。 它已经在SecondViewController.h作为委托方法完成了。

import

导入“SecondViewController.h”

@interface ViewController:UIViewController

//在ViewController.h文件中删除这个方法这是调用ViewController类的简单方法 – (void)getText1:(NSString *)str1 andText2:(NSString *)str2;

@结束