gpt4 book ai didi

ios - 为什么我的委托(delegate)类不起作用?

转载 作者:行者123 更新时间:2023-11-28 22:07:28 25 4
gpt4 key购买 nike

我有3节课

  • 1°仅包含协议(protocol)声明

  • 2°按下保存按钮时调用委托(delegate)方法

  • 3° 实现协议(protocol)中声明的功能并打印“It's Work!”在日志上

这就是我要做的,但它不起作用!!

我试图在 secondo 类中插入这段代码:

NSLog(@"Delegate --> %@", self.delegate);

我在输出中看到这一行:

Delegate --> (null)

我不明白为什么以及哪里错了!!

1° 类代码:

/************************——— NewContoDelegate.h —————************************/
#import <Foundation/Foundation.h>
#import "Conto.h"

@protocol NewContoDelegate <NSObject>

@optional
-(void)insertNewConto:(Conto *)conto;

@end

第二个类创建一个新的 viewController

UIBarButtonItem *doneitem = [[UIBarButtonItem alloc] initWithTitle:@"Salva" style:UIBarButtonItemStyleBordered target:self action:@selector(saveContoPressed)]; 

当按钮被按下时调用委托(delegate)方法:

[self.delegate insertNewConto:newConto];

2°类代码:

/************************———- NewViewController.h —————************************/

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "NewContoDelegate.h"
#import "Conto.h"



@interface NewViewController : UIViewController <UITextFieldDelegate, NewContoDelegate>{



__weak id <NewContoDelegate> _delegate;

}

- (void)saveContoPressed;


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


@end

/************************———- NewViewController.m —————************************/
#import "NewViewController.h"
#import "Conto.h"

@interface NewViewController ()
@end

@implementation NewViewController

@synthesize delegate = _delegate;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
//self.view.backgroundColor = [UIColor lightGrayColor];
self.view.backgroundColor = [UIColor lightGrayColor];






UIBarButtonItem *doneitem=[[UIBarButtonItem alloc]initWithTitle:@"Salva" style:UIBarButtonItemStyleBordered target:self action:@selector(saveContoPressed)];


self.navigationItem.rightBarButtonItem=doneitem;


}
return self;
}

- (void)saveContoPressed
{
if(condition){
… something …

} else {

Conto *newConto = [[Conto alloc] init];

[self.delegate insertNewConto:newConto];
}
}

最后一个类实现了insertNewConto函数

3° 类代码:

/************************———- ListViewController.h —————************************/

#import <UIKit/UIKit.h>
#import "NewContoDelegate.h"
#import "Conto.h"


@interface ListViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, NewContoDelegate> {

}

@end

/************************———- ListViewController.m —————************************/

#import "ListViewController.h"

@interface ListViewController ()
@end

@implementation ListViewController

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

// Custom initialization
self.view.backgroundColor = [UIColor clearColor];





}
return self;
}

-(void)insertNewConto:(Conto *)conto {

NSLog(@“It’s Work! :) ”);


}

解决方案

在 AppDelegate 类中设置委托(delegate):

newViewController.delegate = listViewController;  

最佳答案

像这样设置委托(delegate)

self.listViewController = [[ListViewController alloc] initWithNibName:nil bundle:nil];
self.delegate = self.listViewController;

在你的初始化中。并且必须有人对委托(delegate)实例有很强的引用。

关于ios - 为什么我的委托(delegate)类不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23616563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com