Ray
Ray

Reputation: 31

Passing a NSString Variable from a UIView to a UITableView

Im trying to get the current title from a UIButton declared on a UIViewController.m store it in a NSString and then pass it on to a UITableView controller to use it as a parameter for a NSFetchRequest.

MainMenuViewController.h

    - (IBAction)salesMan:(UIButton *)sender;
    @property (nonatomic,strong) NSString *bPerson;

MainMenuViewController.m

    #import "MainMenuViewController.h"
    #import "DisplayResultsViewController.h"

    // more code here...

    - (IBAction)salesMan:(UIButton *)sender {

        self.bPerson = sender.currentTitle;
    }
    @end

DisplayResultsViewController.h

    @property (nonatomic, strong) NSString *sPerson;

DisplayResultsViewController.m

   - (void)viewDidLoad
   {
        [super viewDidLoad];
        AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
        context = [appDelegate managedObjectContext];

        MainMenuViewController *mainMenu = [[MainMenuViewController alloc]init];

        self.sPerson = mainMenu.bPerson;
        NSLog(@" %@", self.sPerson);

    }

The problem is that it is not passing the variable the NSLog returns "null"

Upvotes: 0

Views: 324

Answers (3)

Mike
Mike

Reputation: 61

I'm not sure if you are using a segue and pushing DisplayResultsViewController but, in your prepare for segue method on MainMenuViewController you can get an instance of the destination view controller which would be DisplayResultsViewController and set the sPerson property there:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    DisplayResultsViewController *drvc = (DisplayResultsViewController *)[segue destinationViewController];
    drvc.sPerson = self.bPerson
}

How are you transitioning between view controllers?

Upvotes: 0

jackdev23
jackdev23

Reputation: 116

One possible solution (not the best) is

In AppDelegate.h (moved from MainMenuViewController.h)

@property (nonatomic,strong) NSString *bPerson;

In MainMenuViewController.m

 - (IBAction)salesMan:(UIButton *)sender {
    AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
        [appDelegate setBPerson:sender.currentTitle];
    }

In DisplayResultsViewController.m

- (void)viewDidLoad
   {
        [super viewDidLoad];
        AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
        context = [appDelegate managedObjectContext];

        MainMenuViewController *mainMenu = [[MainMenuViewController alloc]init];

        self.sPerson = [appDelegate bPerson];
        NSLog(@" %@", self.sPerson);

    }

Upvotes: 0

Anoop Vaidya
Anoop Vaidya

Reputation: 46533

MainMenuViewController *mainMenu = [[MainMenuViewController alloc]init];

A new instance is created in above call line, mainMenu is allocated and initialised with default values.

self.sPerson = mainMenu.bPerson;

self.sPerson is set a value from default initialised mainMenu.bPerson!!!

NSLog(@" %@", self.sPerson);

As expected null will be printed as no value is set.

Possible Solution :

You can use any one of following:

Make the variable global, static, singleton, pass as an argument, post as notification.

Upvotes: 3

Related Questions