문제

I've 3 classes

  • 1° contains only the protocol declaration

  • 2° call the delegate method when the save button is pressed

  • 3° implement the function declared in the protocol and print "It's Work!" on Log

This is what i would do, but it's not working!!

i tried to insert this code in the secondo class:

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

and i see this line in output:

Delegate --> (null)

I don't understand WHY and where i'm wrong!!

1° Class Code:

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

@protocol NewContoDelegate <NSObject>

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

@end

second class create a new viewController with a

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

When the button is pressed call delegate method:

[self.delegate insertNewConto:newConto];

2° Class Code:

/************************———- 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];
           } 
}

The last class implements the insertNewConto function

3° Class Code:

/************************———- 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! :) ”);


}

SOLUTION

Set delegate in AppDelegate class:

newViewController.delegate = listViewController;  
도움이 되었습니까?

해결책

Set the delegate like this

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

in your init. And somebody has to have a strong reference to the delegate instance.

다른 팁

Looks to me like your not setting the ListViewController as the delegate of your NewContoDelegate.

Your accept the protocol but never set the delegate.

I UNDERSTAND!!!!!

As you told me I forgot to set delegate, but i haven't realized that: I must set delegate in the AppDelegate class!!! Thanks for the answers!!

I insert this line in AppDelegate:

newViewController.delegate = listViewController;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top