Question

I am trying to pass a variable value (in my case a simple integer) from one UIViewController ("MainVC") into another ("ChargeVC").

I'm having trouble getting this to work and after a day spent on so reading through the available answers, I now made an account so I'd have a change to get an answer that works for me. I found "This" pretty helpful and thought it should work, but it didn't for me.

I'm obviously new to "xcode" & "Objective-C", but I have solid "PHP" and "Javascript" knowledge.

Here is my code :

MainVC.m

NSUInteger index = 4;  //will be an index ID used for accessing a table row

ChargeVC *myChargeViewCont = [[ChargeVC alloc] init];
myChargeViewCont.title = @"Charge User";
myChargeViewCont.personIndex = index;

NSLog(@"person index MainVC: %d", [myChargeViewCont personIndex]);

[self.navigationController pushViewController:myChargeViewCont animated:YES];

ChargeVC.h

@interface ChargeVC : UIViewController {
    NSUInteger personIndex;
}
@property (nonatomic) NSUInteger personIndex;
@end

ChargeVC.m

#import "ChargeVC.h"
@implementation ChargeVC

@synthesize personIndex;

- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"person index ChargeVC: %d", personIndex);
}

As a check i tried to output the value once in "MainVC" and once in "ChargeVC". Here is the Log:

2014-05-11 12:17:51.242 Kunden[58238:60b] person index ChargeVC: 0
2014-05-11 12:17:51.244 Kunden[58238:60b] person index MainVC: 4

Am I totally missing something here? Any help is appreciated.

Update

I found the error and posted an answer to explain. If you know what exactly happened there, I'd be eager to know.

Was it helpful?

Solution 3

I found my error and it seems I left out a crucial piece of code from my question, or you had probably immediately recognized it. Sorry about that. I'll post what I've learned.

Earlier in "ChargeVC.m" I had set

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.backgroundColor = [UIColor blackColor];
    }
    return self;
}

For reasons I don't fully understand, me setting a value for self.view.backgroundColor there triggered something and the personIndex variable always got lost and/or wasn't accessible during viewDidLoad. As soon as I removed the function, personIndex showed up.

Thanks to rdelmar, who got me on the right track to figuring this out and to all others who helped.

OTHER TIPS

You do not need to initalize it like

    @interface ChargeVC : UIViewController {
    NSUInteger personIndex;
    }
    @property (nonatomic) NSUInteger personIndex;
    @end

Just doing

 @interface ChargeVC : UIViewController
 @property NSUInteger personIndex; 
 @end

is plenty. You can access them in .m by doing self.personIndex. Also, don't bother with nonatomic stuff unless you understand it or is making this for production. That stuff is confusing, and if you're learning it's easier to learn that later on. (I'm assuming hobby project, apologies if this is wrong).

Otherwise, your code seems correct to me.

similar to what Oscar said, I expect there is confusion between your personIndex ivar and your personIndex property.

I would do this, removing the ivar declaration:

@interface ChargeVC : UIViewController
@property ( nonatomic ) NSUInteger personIndex ;
@end

Also, in your @implementation, @synthesize is assumed by default, you can leave that out too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top