1

I need to pass a string from a NSObject class to a UIViewController, I understand that the best way is delegation but the delegate method isn't being called. I'm trying to set the UILabel an DieFacesViewController as the selectedOption from TemporarySelection.

A tableview shows the value of CustomOptionStore, once it's tapped passes its value to TemporarySelection and opens the modal view DieFacesViewCountroller which should, at least in my mind, take the label value from TemporarySelection. The reason I created TemporarySelection is because the DieFacesViewController will be used by other classes, not only by CustomOptionStore, and it will need to load the label from all those classes when different tableViews are selected.

I tried to set the delegate as self in both viewDidLoad and viewWillAppear with no luck, I don't understand if the view loads before being able to call the delegate method or if there's something wrong the way I set the method up.

I've been stuck here for two days, this is the first time I post a question so please forgive me if it's a bit confused.

my delegator class TemporarySelection.h is

#import <Foundation/Foundation.h>
#import "CustomOptionsStore.h"

@class DieFacesViewController;

@protocol TemporarySelectionDelegate <NSObject>

-(void)sendSelection;

@end

@interface TemporarySelection : NSObject

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

@property (nonatomic, strong) NSString *selectedOption;

-(void)addSelection: (CustomOptionsStore *) selection;


@end

and my TemporarySelection.m is

#import "TemporarySelection.h"

@implementation TemporarySelection

-(void)addSelection: (CustomOptionsStore *) selection{

    self.selectedOption = selection.description;

    [self.delegate sendSelection];


}

@end

the delegate class DiewFacesViewController.h is

#import <UIKit/UIKit.h>
#import "SelectedStore.h"
#import "TemporarySelection.h"


@interface DieFacesViewController : UIViewController <TemporarySelectionDelegate>


@property (strong, nonatomic) IBOutlet UILabel *SelectionName;


@end

and the DieFacesViewController.m is

#import "DieFacesViewController.h"

@interface DieFacesViewController ()

@end

@implementation DieFacesViewController


- (void)viewDidLoad {

    TemporarySelection *ts = [[TemporarySelection alloc]init];
    ts.delegate = self;

    [super viewDidLoad];

}

-(void)sendSelection{

    TemporarySelection *ts = [[TemporarySelection alloc]init];
    self.SelectionName.text = ts.selectedOption;

}


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];

} 
Riccardo
  • 51
  • 1
  • 1
  • 7
  • I believe it's bc you are declaring the object `ts` inside a method, or function, therefore when the method is finished, `ts` "falls out of scope" and is no longer existing, therefore the object will not call its delegate – ErickES7 Aug 26 '15 at 13:38

1 Answers1

0

You are not setting the delegate object properly.Check the above code

 #import "DieFacesViewController.h"

@interface DieFacesViewController ()<TemporarySelectionDelegate>
{
//global object

TemporarySelection *ts;
}
@end

@implementation DieFacesViewController


- (void)viewDidLoad {

    ts = [[TemporarySelection alloc]init];
    ts.delegate = self;

    [super viewDidLoad];

}

-(void)sendSelection{

   //Use the object to extract
    self.SelectionName.text = ts.selectedOption;

}


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];

} 
Mukesh
  • 3,680
  • 1
  • 15
  • 32
  • it still doesn't get called – Riccardo Aug 26 '15 at 12:48
  • u may be missing something ,it is getting called at ma end,have u added `TemporarySelectionDelegate` in viewcontroller.and by the way where are u calling `-(void)addSelection: (CustomOptionsStore *) selection` – Mukesh Aug 26 '15 at 12:50
  • I'm calling '(void)addSelection: (CustomOptionsStore *) selection' on the tableView didSelectRowAtIndexPath that that lead to the modal DieFacesViewController. the method gets called, I checked with an NSLog, and it seems to be called before the view is loaded – Riccardo Aug 26 '15 at 13:10
  • As i understand you are doing this while transitioning from one VC to another. Ovevrride prepareForSegue method and set delegate there then call the addSelection. – meth Aug 26 '15 at 13:12
  • I have a tableViewController populated by CustomOptionStore, when a cell from the tableViewController is selected, the value of the cell is passed from CustomOptionsStore to the NSObject class TemporarySelection with didSelectRowAtIndexPath and the view controller DieFacesViewController is loaded. where should I override the prepareForSegue, on the tableViewController? or on TemporarySelection? and how? Sorry for the pain, I'm quite new at Objective c and delegates are driving me crazy... – Riccardo Aug 26 '15 at 15:00
  • you should do it in tableViewController. it performs segue to another VC. You can pass objects from one vc to another or make creation of objects there. [this link](http://stackoverflow.com/questions/7864371/how-to-pass-prepareforsegue-an-object) may help. – meth Aug 26 '15 at 15:09