Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 27, 2011, 03:19 AM   #1
multinode
macrumors regular
 
Join Date: Feb 2011
Remotely accessing an instance variable

"nextPath" is an instance variable in OrderEntryAppDelegate. It's value in the start method in OrderEntryAppDelegate.m is "/Subjects". However, when it's used in SubjectsController.m, it's value is nil. Why? What did I do wrong please?

Code:
OrderEntryAppDelegate.h

#import <UIKit/UIKit.h>
#import "SubjectsController.h"

@interface OrderEntryAppDelegate : NSObject <UIApplicationDelegate> {
	UIWindow *window;
	UIViewController *welcomeController;
	SubjectsController *subjectsController;
	UINavigationController *navigationController;
	NSString *pathToHere, *nextPath;
	UIButton *startButton;
	}
-(IBAction) start:(id)sender;

@property (nonatomic, retain) IBOutlet UIButton *startButton;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UIViewController *welcomeController;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSString *pathToHere, *nextPath;

@end

OrderEntryAppDelegate.m

#import "OrderEntryAppDelegate.h"
#import "SubjectsController.h"
#import "UIKit/UIKit.h"

@implementation OrderEntryAppDelegate

@synthesize window;
@synthesize welcomeController;
@synthesize startButton;
@synthesize navigationController;
@synthesize pathToHere;
@synthesize nextPath;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
	(NSDictionary *)launchOptions {

	pathToHere = @"/";

	//activate the start button
	[startButton setHidden:NO];

	//create navigation controller
	navigationController = 
		[[UINavigationControlleralloc] initWithRootViewController: welcomeController];
 	[self.window addSubview:navigationController.view];
	[self.window makeKeyAndVisible];

	return YES;
	}


-(void) start:(id)sender{
	//create myTableViewController instance
	subjectsController = [[SubjectsController alloc] initWithStyle:UITableViewStylePlain];
	nextPath = [pathToHere stringByAppendingPathComponent:@"Subjects"];
	[navigationController pushViewController:subjectsController animated:YES];
	printf("The start button was tapped\n");
	}

								
- (void)dealloc {
	[window release];
	[super dealloc];
	}
@end


SubjectsController.h

#import <UIKit/UIKit.h>
#import "OrderEntryAppDelegate.h"

@interface SubjectsController : UITableViewController {
	NSString *pathToHere;
	NSArray *dataArray;
	}

@property (nonatomic, retain) NSString *pathToHere
@property (nonatomic, retain) NSArray *dataArray;
@end

SubjectsController.m

#import "OrderEntryAppDelegate.h"
#import "SubjectsController.h"

@implementation SubjectsController

@synthesize pathToHere;
@synthesize dataArray;

- (id)initWithStyle:(UITableViewStyle)style {
	self = [super initWithStyle:style];
	if (self) {
		// Custom initialization.
		self.title = @"Conference Subjects";		
		}
	return self;
	}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	OrderEntryAppDelegate *delegate = [[UIApplication sharedApplication]delegate];
	NSError *error = nil;
	self.pathToHere = [delegate nextPath];
	dataArray = [[NSFileManager alloc] contentsOfDirectoryAtPath:delegate.pathToHere 
		error:&error];
	return [dataArray count];
	}


-(UITableViewCell *) tableView:tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	OrderEntryAppDelegate *delegate = [[UIApplication sharedApplication]delegate];
	NSError *error = nil;
	self.pathToHere = [delegate nextPath];
	dataArray = [[NSFileManager alloc] contentsOfDirectoryAtPath:delegate.pathToHere 
		error:&error];
	UITableViewCell *cell = 
		[self.subjectsViewdequeueReusableCellWithIdentifier:@"UITableViewCell"];
	if (!cell) {
		cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
			reuseIdentifier:@"UITableViewCell"] autorelease];
		}
	cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
	return cell;
	}


/*-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	OrderEntryAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
	delegate.nextPath = [pathToHere stringByAppendingPathComponent:selectedCell.textLabel.text];
	conferencesController = [[ConferencesController alloc] initWithStyle:UITableViewStylePlain];
	[delegate.navigationController pushViewController:conferencesController animated:YES];
	printf("next Level\n");
	}*/
@end
__________________
PLEASE DON'T SUPPORT ANTI AMERICA WIKILEAKS ... CUTE BUT DESTRUCTIVE

Last edited by multinode; May 27, 2011 at 06:29 AM.
multinode is offline   0 Reply With Quote
Old May 27, 2011, 04:27 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
I've not read all your code but this is the first thing that jumps out at me:

You are not using your synthesized accessor to set nextPath.

Code:
nextPath = [pathToHere stringByAppendingPathComponent:@"Subjects"];
is not the same as (or even remotely similar to)

Code:
self.nextPath = [pathToHere stringByAppendingPathComponent:@"Subjects"];
In the first case you directly set the variable to point to an autoreleased object. You should expect that this will get released at the end of the current run loop. This is what is happening and therefore is the expected behaviour.

In the second case you use some syntactic sugar to call the setter method. This setter method will (due to your property declaration and synthesize call) retain the object and the set the pointer. So the object will not get released at the end of the current run loop.

The lesson to take out of this is never directly set properties. Always use the accessor methods via self. or calling setXXX directly. This is also why it's a good idea to give the variable a different name to the property (convention is to call the variable _propertyName) and use the synthesize directive to make that work. This prevents you making this error.
robbieduncan is offline   0 Reply With Quote
Old May 27, 2011, 05:41 AM   #3
multinode
Thread Starter
macrumors regular
 
Join Date: Feb 2011
Thanx Robbie. Your correction fixed all my problems. Hours and hours and hours of thinking and thinking and thinking.
__________________
PLEASE DON'T SUPPORT ANTI AMERICA WIKILEAKS ... CUTE BUT DESTRUCTIVE
multinode is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Instance variable declaration: best practices? MacMan988 iPhone/iPad Programming 1 Jan 26, 2014 07:21 AM
Resolved: Need Help Accessing OS X Server Remotely adarkenigma Mac OS X Server, Xserve, and Networking 2 Oct 30, 2013 12:23 AM
Accessing instance variables.. Apple approved way? Tander iPhone/iPad Programming 4 Jul 4, 2013 07:05 AM
Accessing a static variable from an instance method nerak99 iPhone/iPad Programming 25 Sep 13, 2012 11:26 AM
Remotely accessing files MadDoc Mac OS X 10.7 Lion 4 Jun 26, 2012 04:40 PM

Forum Jump

All times are GMT -5. The time now is 03:08 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC