gpt4 book ai didi

ios - self.delegate 总是 nil

转载 作者:行者123 更新时间:2023-12-01 20:03:52 24 4
gpt4 key购买 nike

我看到了类似的问题。在他们说要使用className.delegate = self的所有地方或其他类似的解决方案。但我已经检查过我所做的一切都是正确的。然后我也面临着 self.delegate 是零问题。

我正在尝试在按钮单击时将数据从 MyViewController 传递到 NextViewController

我做到了,

1.MyViewController中创建协议(protocol)

2.在MyViewController中创建委托(delegate)实例

3.在按钮单击事件中编写此代码

-(void)buttnPressed:(id)s{

NextViewController* d=[[NextViewController alloc]init];

if(self.delegate){
NSLog(@"D");
}
if (self.delegate && [self.delegate respondsToSelector:@selector(groupSelected:)]) {
[self.delegate groupSelected:@"df"];
}
[self.navigationController pushViewController:d animated:YES];
}

4.在NextViewController中实现FirstViewController并设置为delegate
5.Created FirstViewController 的实例(在 NextVC 内)并使用该实例我将它的委托(delegate)设置为 self
6.实现NextVC中的委托(delegate)方法

据我所知,我做的一切都是正确的,但我没有得到解决方案。请帮忙。谢谢你的时间。 (:

这是我的代码

MyViewController.h
#import <UIKit/UIKit.h>


@protocol WatchListDelegate <NSObject>

-(void)groupSelected:(NSString *)grouDetails;

@end


@interface MyViewController : UIViewController
@property (strong,nonatomic) id<WatchListDelegate> delegate;

@end

MyViewController.m
#import "MyViewController.h"
#import "NextViewController.h"

@interface MyViewController ()
@property UIButton *buttonPopUp;
@end

@implementation MyViewController

-(id)init{
self = [super init];
return self;
}

-(void)loadView{
[super loadView];
self.buttonPopUp=[UIButton buttonWithType:UIButtonTypeCustom];
[self.buttonPopUp addTarget:self
action:@selector(buttnPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.buttonPopUp setTitle:@"Press ME" forState:UIControlStateNormal];
[self.buttonPopUp setBackgroundColor:[UIColor blueColor]];
self.buttonPopUp.frame=CGRectMake(80,180,80,40);
[self.view addSubview:self.buttonPopUp];
}


-(void)buttnPressed:(id)s{

if(self.delegate){
NSLog(@"D");
}
if (self.delegate && [self.delegate respondsToSelector:@selector(groupSelected:)]) {
[self.delegate groupSelected:@"df"];
}
NextViewController* d=[[NextViewController alloc]init];
[self.navigationController pushViewController:d animated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
@end

NextViewController.h
#import <UIKit/UIKit.h>
#import "MyViewController.h"

@interface NextViewController : UIViewController<WatchListDelegate>

@end

NextViewController.m
#import "NextViewController.h"

@interface NextViewController ()

@end

@implementation NextViewController


-(id)init{
self = [super init];
if(self){
MyViewController *m=[[MyViewController alloc]init];
m.delegate = self;
}
return self;
}

-(void)loadView{
[super loadView];

}


- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}


-(void)groupSelected:(NSString *)grouDetails{
NSLog(@"groupSelected");
}
@end

感谢您的时间(:

最佳答案

试试这个:

-(void)buttnPressed:(id)s{

NextViewController* d= [self.storyboard instantiateViewControllerWithIdentifier:@"VCidentiferFromStoryBoard"];
self.delegate = d;
[self.navigationController pushViewController:d animated:YES];
if (self.delegate && [self.delegate respondsToSelector:@selector(groupSelected:)]) {
[self.delegate groupSelected:@"df"];
}
}

关于ios - self.delegate 总是 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39447891/

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