【问题标题】:Delegate method not being called objective C委托方法不被称为目标C
【发布时间】:2016-03-22 12:34:53
【问题描述】:

我正在尝试将 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

查看控制器.h

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

@interface ViewController : UIViewController<SecondViewDelegate>


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

查看控制器.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

【问题讨论】:

    标签: ios objective-c delegates


    【解决方案1】:

    问题是您创建了两个SecondViewController 对象,并让您的ViewController 成为错误对象的代表。

    这:[[SecondViewController alloc] init] 在代码中创建一个对象。这:[self performSegueWithIdentifier:@"moveToSecondController" sender:self] 根据情节提要定义创建一个对象。

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

    【讨论】:

      【解决方案2】:

      尝试在 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;
      
          }
      }
      

      【讨论】:

      • 这里的 sv 是什么。能详细解释一下吗
      【解决方案3】:

      您无需在ViewController.h 中声明您的delegate method。已经在SecondViewController.h作为委托方法完成了。

      【讨论】:

        【解决方案4】:

        进口

        导入“SecondViewController.h”

        @interface ViewController : UIViewController

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

        @结束

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-10-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-26
          • 2016-11-10
          • 1970-01-01
          相关资源
          最近更新 更多