委托方法没有被调用,设置委托给自己?

问题描述:

所以我试图弄清楚使用委托,我已经看了一些关于如何使用它们的教程。我仍然觉得它们很混乱,在尝试自己实现一个之后,有一个我似乎无法解决的问题。委托方法没有被调用,设置委托给自己?

我有两个ViewControllers,第一个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的新手,所以如果你能解释你在说什么,那会很棒。谢谢。

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

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

SecondViewController需要有参考FirstViewController能够为自身设置为代表。

+0

啊,好的。做一个参考,[我必须做这样的事情](http://*.com/questions/9099961/objective-c-getting-a-uiviewcontroller-reference)我认为?但是我没有父/子结构,所以我必须以不同的方式设置viewcontroller。编辑:等等,很确定我可以做到这一点时呈现viewcontroller2 –

+0

这应该工作,但既然你提到你已经建立了一个segue推'SecondViewController',你可以使用'prepareForSegue'方法来检索'destinationViewController'(即你的'SecondViewController')。检查这个问题http://*.com/questions/7864371/ios-how-to-pass-prepareforsegue-an-object – lucianomarisi

+0

是的,得到它与'self.delegate = secondVC;'设置目标vc后工作。非常感谢!! –

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

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