iOS - 代表不工作

标签 ios objective-c

我有两个 View Controller ,AbcViewControllerXyzViewController。两个 Controller 的行为相似。每个都有一个“添加”按钮,可分别打开 AddNewAbcViewControllerAddNewXyzViewController

AddNewAbcViewController 上,当点击“提交”按钮时,它会执行必要的操作并关闭,将其带回 AbcViewController。我在这里使用委托(delegate),其中 AbcViewController 关闭 AddNewAbcViewController。这行得通。

现在我想对 XyzViewControllerAddNewXyzViewController 执行相同的操作,但它不起作用。 AddNewXyzViewController中调用btnSubmit时,并没有进入XyzViewController的dimiss方法。我已经多次扫描我的代码,但没有发现任何未添加的额外内容。我什至在 XyzViewControllerAddNewXyzViewController 中给出了不同的关闭方法名称,但这也不起作用。我错过了什么?

这是我的 AbcViewControllerAddAbcViewController 片段。 Xyz 的代码是相同的:

AddNewAbcViewController.h

#import <Foundation/Foundation.h>

// protocol
@protocol AddNewAbcProtocol <NSObject>

-(void)dismiss;

@end

@interface AddNewAbcViewController : UIViewController<UITextViewDelegate>

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

@end

AddNewAbcViewController.m

@interface AddNewAbcViewController() <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
...
@end

@implementation AddNewAbcViewController

...

- (IBAction)btnSubmit:(id)sender
{
  [self.delegate dismiss];
}
@end

AbcViewController.h

#import <Foundation/Foundation.h>
#import "AddNewAbcViewController.h"

@interface AbcViewController : UIViewController<AddNewAbcProtocol, UISplitViewControllerDelegate>
...
@end

AbcViewController.m

@implementation AbcViewController

-(void)dismiss
{
    NSLog(@"delegated to dismiss()");
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

最佳答案

正如大家所解释的,基本上你忘记了一行代码,如“.delegate = self”。

这是一个方便的初学者介绍给代表。

https://stackoverflow.com/a/4213005/294884

关于iOS - 代表不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26524815/

相关文章:

ios - 在 ios 应用程序中使用 facebook 应用程序登录在 safari "Login with Facebook app"中显示选项并且它在 ios 10.1 中不起作用

ios - 无法将逗号分隔的字符串转换为数组

ios - 无法在 Collection View 中选择项目

ios - 我已经隐藏了导航栏和状态栏,现在屏幕边缘平移手势返回不起作用,这是典型的吗?

iphone - 从 UIViewController 调用 UIApplicationDelegate 方法

objective-c - 你能以编程方式调用 siri 吗?

c++ - 针对 libstdc++ 和 libc++ 链接 iOS 应用程序

javascript - 解析 CloudCode 函数不起作用。调用它时出现错误

iOS系统按钮点击框问题

objective-c - 使用描述打印结构