ios - self.delegate 总是 nil

标签 ios objective-c delegates

我看到了类似的问题。在他们说要使用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/

相关文章:

ios - 填充数组未给出预期结果

ios - dispatch_queue_t 定义为强

ios - 我如何在 DDMathParser 中设置 % 模式开关

objective-c - 隐藏在 iOS 9 中的附件栏

java - Java 中的条件委托(delegate)

iOS 应用程序 : Preview Layer freezes after Recording button is clicked

ios - 在 Cordova/Ionic/iOS 中滚动时的空白区域

objective-c - 如何确定 UIColor 是否基于模式?

objective-c - 为什么 Objective-C 中会自动调用这个委托(delegate)方法?

iphone - 是否为 Core Data 的获取结果 Controller 实现委托(delegate)