【问题标题】:Delegate method not being called, setting delegate to self?未调用委托方法,将委托设置为自我?
【发布时间】:2014-06-15 19:03:03
【问题描述】:

所以我正在尝试掌握使用委托的技巧,到目前为止,我已经观看了一些关于如何使用委托的教程。我仍然觉得它们令人困惑,并且在尝试自己实现一个之后,有一个我似乎无法解决的问题。

我有两个 ViewController,第一个 ViewController 包含一个 UITextField *sampleTextField 和一个带有方法 switchViews 的按钮。它还包含带有sendTextToViewController 方法的协议声明。 SwitchViews 还链接到切换到SecondViewController 的segue。在SecondViewController 中唯一的对象是UILabel *outputLabel 当用户点击按钮时,它调用switchViews 并且视图更改为SecondViewController,并且在加载时outputLabel 应该更改为在sampleTextField 中输入的任何文本ViewController。然而,委托方法sendTextToViewController 永远不会被调用。所有对象都在 Interface Builder 中创建。

下面是让代码更容易理解的代码:

ViewController.h

#import <UIKit/UIKit.h>

@protocol TextDelegate <NSObject>

-(void)sendTextToViewController:(NSString *)stringText;

@end

@interface ViewController : UIViewController

- (IBAction)switchViews:(id)sender;

@property (weak, nonatomic) IBOutlet UITextField *sampleTextField;

@property (weak, nonatomic) id<TextDelegate>delegate;

@end

然后在 ViewController.m

中声明
- (IBAction)switchViews:(id)sender {
    NSLog(@"%@", self.sampleTextField.text);
    [self.delegate sendTextToViewController:self.sampleTextField.text];
}

SecondViewController.h

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

@interface SecondViewController : UIViewController <TextDelegate>

@property (weak, nonatomic) IBOutlet UILabel *outputLabel;


@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController
@synthesize outputLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    ViewController *vc = [[ViewController alloc]init];
    [vc setDelegate:self];
}

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

-(void)sendTextToViewController:(NSString *)stringText
{
    NSLog(@"Sent text to vc");
    [outputLabel setText:stringText];
}

I've looked at this 和第一个答案是有道理的,但由于某种原因它不起作用。

我确实认为问题出在我设置调用[vc setDelegate:self] 的位置,但不知道如何解决这个问题。一些正确方向的指针将不胜感激。请记住,我是 obj-c 的新手,所以如果你能解释你在说什么,那就太好了。谢谢。

【问题讨论】:

    标签: ios iphone objective-c delegates


    【解决方案1】:

    您正在创建一个 ViewController 的新实例,但您没有对它做任何事情。

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        ViewController *vc = [[ViewController alloc]init];
        [vc setDelegate:self];
    }
    

    SecondViewController 需要引用 FirstViewController 才能将自己设置为委托。

    【讨论】:

    • 嗯,好的。作为参考,I'd have to do something like this 我假设?但是我没有父/子结构,所以我必须以不同的方式设置视图控制器。编辑:等等,很确定我可以在展示 viewcontroller2 时做到这一点
    • 应该可以,但是既然你提到你已经设置了一个 segue 来推送SecondViewController,你可以使用prepareForSegue 方法来检索destinationViewController(即你的SecondViewController)。检查这个问题stackoverflow.com/questions/7864371/…
    • 是的,在设置目标 vc 后,它可以与 self.delegate = secondVC; 一起使用。非常感谢!!
    • 我想知道如果没有 segues 我该怎么做呢?由于我没有父/子结构,我不能使用我链接的方法,我必须做一些不同的事情,比如为 ViewController2 声明一个@property,然后以某种方式在 ViewController 中使用该属性。唔。有什么想法吗?
    • 如果您没有使用 segues,您可能会从 FirstViewController 推送导航堆栈上的 SecondViewController,此时您将设置委托。
    【解决方案2】:

    首先,您不必使用委托来执行这样的程序。 更简单的方法是在 SecondViewController 中创建一个属性,将 textField 的内容传递给它。

    您的代码不起作用,因为您在尚未设置的委托上调用了 sendTextToViewController。您已将委托设置为 ViewController 的一个新实例,而不是屏幕上显示的那个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多