【问题标题】:Delegate method in the first view does not get invoked from the second view第一个视图中的委托方法不会从第二个视图中调用
【发布时间】:2012-07-01 19:00:06
【问题描述】:

在一个视图中,我们称它为 firstView 我创建了一个 secondView,如下所示,如果 firstView 中发生了某些事情,则将其推送:

SecondViewController *secondVC = [[secondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];


    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];

现在,当我在 secondView 中时,如果按下按钮,我想返回到 firstView 并将一个值从 secondView 传递回 firstView(假设从 secondView 到 firstView 的文本字段的整数值)。

这是我尝试过的:

@protocol SecondViewControllerDelegate;

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

@interface SecondViewController : UIViewController <UITextFieldDelegate>
{
    UITextField *xInput;
    id <SecondViewControllerDelegate> delegate;
}

- (IBAction)useXPressed:(UIButton *)sender;

@property (assign) id <SecondViewControllerDelegate> delegate;

@property (retain) IBOutlet UITextField *xInput;

@end

@protocol SecondViewControllerDelegate
- (void)secondViewController:(SecondViewController *)sender xValue:(int)value;

@end

在m文件中

- (IBAction)useXPressed:(UIButton *)sender
{
    [self.delegate secondViewController:self xValue:1234]; // 1234 is just for test
}

然后在第一个视图中我做了:

#import "SecondViewController.h"

@interface FirstViewController : UITableViewController <SecondViewControllerDelegate> {

}

@end

并实施:

- (void) secondViewController:(SecondViewController *)sender xValue:(int)value
{
    [self.navigationController popViewControllerAnimated:YES];
}

现在,问题在于 FirstViewController 中的一个我收到警告“没有找到协议“SecondViewControllerDelegate”的定义,并且对于两个委托方法(上面的最后一段代码)根本没有被调用。有人可以请告诉我怎么了?

【问题讨论】:

  • 您确定委托!= nil?
  • 我应该做什么以及在哪里确保代表不为零?
  • 你设置了你的委托吗?像 secondVC.delegate = self;

标签: ios delegates uinavigationcontroller


【解决方案1】:

这一行之后

SecondViewController *secondVC = [[secondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

添加

secondVC.delegate = self;

也代替

- (void) secondViewController:(SecondViewController *)sender xValue:(int)value
{
    [self.navigationController popViewControllerAnimated:YES];
}

你应该使用

- (void) secondViewController:(SecondViewController *)sender xValue:(int)value
{
    [sender popViewControllerAnimated:YES];
}

【讨论】:

  • 非常感谢 Omar Abdelhafith!设置委托确实是解决方案。
  • 如果有一个 ZerothViewController 以模态方式呈现 FirstViewController 将如何更改设置委托。基本上我希望 SecondViewController 将数据传递给 ZerothViewController 但我无法找出正确的说法。你能帮忙吗?
【解决方案2】:

FirstViewController.h 文件中:

#import "SecondViewController.h"

@interface FirstViewController : UITableViewController <SecondViewControllerDelegate> {

SecondViewController *secondViewController;

}

@end

在实现文件中,您在其中初始化 SecondViewController 实例的下一行将 self 分配给委托属性:

secondViewController.delegate = self;

接下来定义委托方法:

- (void)secondViewController:(SecondViewController *)sender xValue:(int)value
{
NSLog ("This is a Second View Controller with value %i",value)
}

【讨论】:

  • 非常感谢 mancunianetz!我希望我可以选择更多这些作为正确答案。你说的确实是解决办法。再次感谢!
【解决方案3】:

对于问题 1:SecondViewControllerDelegate@protocol 定义看起来像是在 secondViewController.h 中;你确定这个文件是在firstViewController.h 中导入的吗?否则它不会知道协议。

问题 2:它可能与问题 1 完全无关。您确定该操作已正确连接吗?您能否在您的 useXPressed: 中调用 NSLog() 以确保该方法实际上在您期望的时候被调用?

【讨论】:

  • 问题一:是的,FirstViewController中导入了SecondViewController。问题 2:我放了一个断点,是的,程序进入 useXPressed: 但没有进入 - (void)secondViewController:(SecondViewController *)sender xValue:(int)value;因为我也在那里设置了一个断点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多