Value passing problem from once class to another

Joined
Jun 3, 2011
Messages
54
Reaction score
0
Points
6
Dear All

I am able to assign values to NSarray timeZoneNames defined in RootViewController.m from SimpleTableViewAppDelegate.m and then I can use 'timeZoneNames' inside RootViewController.m perfectly.

But When the same procedure I follow for SubTable.m (where Nsarray timevalue is defined), a null array is assigned in SubTable.m

Codes are :

SimpleTableViewAppDelegate.m

Code:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
	
	RootViewController *rootViewController = [[RootViewController1 alloc] initWithStyle:UITableViewStylePlain];
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	
SubTable *sub = [[SubTable alloc] initWithStyle:UITableViewStylePlain];
NSArray *timeZones = [NSTimeZone knownTimeZoneNames];

rootViewController.timeZoneNames = [timeZones sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
	
	
  NSArray *test_array = [[NSArray alloc] initWithArray:timeZones];
  sub.timevalue = [test_array copy];
	//NSLog(@"count is %d", [sub.timevalue count]);
	
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
	self.navigationController = aNavigationController;
	[aNavigationController release];
	[rootViewController release];
	[window addSubview:[navigationController view]];
	[window makeKeyAndVisible];
}

RootViewController.h is like:

Code:
@interface RootViewController : UITableViewController {
	NSArray *timeZoneNames;
}

@property (nonatomic, retain) NSArray *timeZoneNames;

@end

And inside RootViewController.m ,
Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSLog(@"timezonesname count is %d", [timeZoneNames count]);
	return [timeZoneNames count];
}

In above case, correct value is getting displayed.

While
SUbTable.h looks like:

Code:
@interface SubTable: UITableViewController {
	
	NSArray *timevalue;

}
@property (nonatomic ,retain) NSArray *timevalue;
@end

and Inside SUbTable.m
Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSLog(@"timevalue count is  %d", [timevalue count]);
    return [timevalue count];
}

And here value displayed is always 0.

Where exactly I am making the mistake? What should I do so that timevalue can be assigned in the same way timeZoneNames is assigned.
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
Ok, I must be missing something because I'm tired, but I see where you instantiate the SUbTable object, but I don't see where you're actually pushing it onto the navigation stack to become visible.

My guess is (and it's just a guess), and I can't verify this because I don't see where you're pushing it on - you're re-instantiating it (probably inside of rootViewController?) and pushing it on and that new instance you're not setting the timevalue ivar for that new instance (since multiple instances of a single class won't share instance variables, instance variables are unique for each instance of a class)...
 
OP
S
Joined
Jun 3, 2011
Messages
54
Reaction score
0
Points
6
Hi

I have not re-instantiated the SUbTable object. It has been instantiated only once and that in application delegate class.

Moreover I just tried making a replica of RootViewController (in form of SubTable) but output is not coming as expected.

Is it something that I should instantiate the SubTable classs object inside RootViewController.m or is it something that scope of object is valid only in current view?? [ Subtable is the next view(It comes when any cell of RootViewController is tapped)].
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
Well, where's the code that actually pushes the sub table view controller? How are you referencing it from the app delegate? I see your base instantiation, but from the scope, it will only exist in the method:

Code:
 - (void)applicationDidFinishLaunching:(UIApplication *)application
- no where do you pass the sub view controller to your root view.

Usually when using a nav controller, when you need to push a new view, it's instantiated and has its ivars set just before pushing onto the view controller, not at the point of the app delegate. The only view controller that should be instantiated at the app delegate at launch is the root view controller and any controllers the root view controller requires to operate (ie: navigation view controller as the root, and then its initial view controller) - there are only rare times where you'd need more then those required view controllers (like if you're outputting to an external screen through your app).

Unfortunately, there isn't enough code shown to fully see how you are pushing your sub view onto the stack and how you think you're accessing it, which would seem at this point you're not accessing at all (btw - with what you've shown, you leak that view controller)...
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top