ios - 第一个 View 中的委托(delegate)方法不会从第二个 View 中调用

标签 ios delegates uinavigationcontroller

在 View 中,我们称其为firstView,我按如下方式创建了secondView,并在firstView中发生某些事情时推送它:

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


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

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

这是我尝试过的:

@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
}

然后在第一个 View 中我做了:

#import "SecondViewController.h"

@interface FirstViewController : UITableViewController <SecondViewControllerDelegate> {

}

@end

并实现:

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

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

最佳答案

此行之后

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

关于ios - 第一个 View 中的委托(delegate)方法不会从第二个 View 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11284831/

相关文章:

ios - 如何在 Swift 中重写协议(protocol)扩展的实例方法?

ios - 排序 ABMultiValueRef(kABPersonPhoneProperty)

c# - 在方法中创建委托(delegate)类型

ios - 在导航标题下添加搜索栏

ios - 如何从 xcode 卸载 swiftlint,或关闭它的警告

ios - 如何检测我的 AppDelegate 中的暗/亮模式更改以重置全局 tintColor?

c++ - 如何制作Qt委托(delegate)编辑器 'sticky'

swift - SceneKit Swift 2.0 碰撞检测(SCNPhysicsContactDelegate)

ios - 如何将UISearchbar做成矩形并与导航栏合并?

swift - 当父级是标签栏 View Controller 时,如何嵌入没有标签栏的 View Controller ?