Emmie Capps Emmie Capps - 2 months ago 9
iOS Question

self.delegate is always nil

I saw similar questions like this. In all the place they said to use

className.delegate = self
or other similar solutions. But i already checked i have did everything correctly. Then also i am facing self.delegate is nil problem.

I am trying to pass data from MyViewController to NextViewController on Button Click

I did,

1.Created Protocol in MyViewController

2.Created delegate Instance in MyViewController

3.Wrote this code in Button Click Event

-(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.Implemented FirstViewController in NextViewController and set it's delegate
5.Created FirstViewController's instance (inside NextVC) and using that instance i set it's delegate as self
6.Implemented the delegate method in NextVC

I did everything correctly to my knowledge but i did not get the solution. Please help. Thanks for the time. (:

Here is my code

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


Thanks for the time (:

Answer

Try This:

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