Search code examples
iosobjective-cnsstringparameter-passingecslidingviewcontroller

Passing NSString from one class to another. (ECSlidingViewController?)


Firstly, I've already tried to search for solutions online but none works for me and I'm thinking since I'm using ECSlidingViewController to navigate around the app, I can't utilise the prepareForSegue method thus, my problem may need a different approach.

  1. I have a class called viewInits which holds properties in the .h file that I want allow other classes to set and get it's values. In this case, the property is an NSString *navBarTitle.

  2. In ClassA, I have a tableView:didSelectRowAtIndexPath: method, where I

    1. Create an ViewInits class object - *viewInits.
    2. I then set the setNavBarTitle: to the value of [self.MenuRowsArray objectAtIndex:indexPath.row].
    3. In the next line, I did an NSLog to check and yes, viewInits.navBarTitle now holds the value I desire.
  3. In ClassB's viewDidloadMethod, similarly, I created a ViewInits object - *viewInits and did an NSLog check for viewInits.navBarTitle. But it returns (null). What seems to be the problem here?


Here is the code for how I'm trying to pass the NSString. What am I doing wrong?

viewInit .h

@interface ViewInits : NSObject
@property (strong, nonatomic) NSString *navBarTitle;
@end

ClassA.m tableView:didSelectRowAtIndexPath: method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = [self.MenuRowsArray objectAtIndex:indexPath.row];
    UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];

    // *---------- Assign identifier to NSString viewInits ----------*
    ViewInits *viewInits = [[ViewInits alloc] init];
    [viewInits setNavBarTitle:identifier];
    NSLog(@"%@", viewInits.navBarTitle);
    // *---------- Assign identifier to NSString viewInits ----------*

    [self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^
    {
        CGRect frame = self.slidingViewController.topViewController.view.frame;
        self.slidingViewController.topViewController = newTopViewController;
        self.slidingViewController.topViewController.view.frame = frame;
        [self.slidingViewController resetTopView];
    }];
}

ClassB.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // *========== ECSlidingViewController ==========*
    self.view.layer.shadowOpacity = 0.75f;
    self.view.layer.shadowRadius = 10.0f;
    self.view.layer.shadowColor = [UIColor blackColor].CGColor;
    if (![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]])
    {
        self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Menu"];
    }
    [self.view addGestureRecognizer:self.slidingViewController.panGesture];
    // *========== ECSlidingViewController ==========*


    ViewInits *viewInits = [[ViewInits alloc] init]; // Create ViewInit class object
    self.navBar.topItem.title = viewInits.navBarTitle;
    NSLog(@"%@", viewInits.navBarTitle); // <<--- This always ends up null. What's wrong?
}

Your help are much appreciated. Thank you.


Solution

  • If you want to use ViewInit as a common store of settings it should be a singleton so that all other instances in the app can get it. Currently you're creating a new instance each time you want to use it, so the new instance doesn't have any of your previous settings.

    Aside, I know what the sliding view controller is, I ask about it because you may be using it incorrectly. If you have a view controller which is the current top view controller and it changes the top view controller (class A might be doing this, not sure) then the reference self.slidingViewController will stop working part way through your code.