Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

2Cheap2Switch

macrumors member
Original poster
May 16, 2005
53
0
Stanford
So I'm writing an app that uses the same view controller multiple times, however whenever I allocate it, change the text of a label in the controller, and then push it onto the navigationController, the label stays exactly the same. I have a uilabel in the xib file linked up with the IBOutlet in the infoController class definition. However nothing comes up when I actually run in the simulator.

Here is what infoController looks like:

Code:
@interface infoController : UIViewController {
             IBOutlet UILabel * infoLabel;
}

-(void) setInfoString: (NSMutableString*) infoString;

@end

@implementation infoController

-(void) setInfoString: (NSMutableString *) infoString{
            infoLabel.text = infoString;
}

@end

This is actually how I'm allocating and changing the text of the label in the infoController instance.

Code:
infoController * newController = [[[infoController alloc] initWithNibName:@"infoController" bundle:nil] autorelease];
[newController setInfoString:dataString];
[[self navigationController] pushViewController:newController animated:YES];


I've done this many times and have no clue why it's not working now. Any help would be greatly appreciated.
 
Imagine infoLabel is nil. Describe what would happen with your code?

How would you obtain evidence that infoLabel isn't nil?
 
In the debugger/gdb it shows up as nil. To my understanding, if an element is linked up with an item in the .xib, then it is actually allocated and instantiated when the view controller is allocated and initialized with that xib. I have also tried creating my own initializer where I actually assign info to [[uilabel alloc] init] but I get the same exact result.
 
To my understanding, if an element is linked up with an item in the .xib, then it is actually allocated and instantiated when the view controller is allocated and initialized with that xib.

Suppose this is correct.
What does it suggest as a possible underlying problem?
In other words, what is the next logical thing to check?
Hint: it involves the xib's outlet connection.


I have also tried creating my own initializer where I actually assign info to [[uilabel alloc] init] but I get the same exact result.
Well that can't possibly work, because "UILabel" is misspelled. Therefore, it can't be real code. Post your real code.

Maybe you're assigning a value to the variable in the wrong place. We'd only be able to see that if you post your real code.

Loading from an xib or nib doesn't invoke the usual initializer method.
See initWithCoder: in the NSCoding protocol.
http://developer.apple.com/iphone/l...ls/NSCoding_Protocol/Reference/Reference.html
http://developer.apple.com/iphone/l...conceptual/Archiving/Tasks/codingobjects.html
 
This is the function that is being called. This is inside of a view controller that is already inside of a uinavigationcontroller.

Code:
-(IBAction) showStats:(id) sender{
	NSMutableString *dataStringToUse = nil;
	switch ([sender tag]) {
		case 0:
			dataStringToUse = logicalString;
			break;
		case 1:
			dataStringToUse = switchString;
			break;
		case 2:
			dataStringToUse = islString;
			break;
		case 3:
			dataStringToUse = portString;
			break;
		case 4:
			dataStringToUse = endDevicesString;
			break;
	}
	infoController * newView = [[[infoController alloc] initWithNibName:@"infoController" bundle:[NSBundle mainBundle]] autorelease];
	
	[newView setInfoString:dataStringToUse];
	[[self navigationController] pushViewController:newView animated:YES];
}


Here is the definition and implementation of infoController


Code:
@interface infoController : UIViewController {
	IBOutlet UILabel * infoLabel;
}

-(void) setInfoString:(NSMutableString *) infoString;

@end



#import "infoController.h"


@implementation infoController



-(void) setInfoString:(NSMutableString *)infoString{
	//infoLabel = [[UILabel alloc] init];
	infoLabel.text = infoString;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end


I just checked and the outlet connection from the file's owner to the actual uilabel in the view is fine.
 
Code:
-(void) setInfoString:(NSMutableString *)infoString{
	//infoLabel = [[UILabel alloc] init];
	infoLabel.text = infoString;
}
If the alloc & init line were uncommented, then a UILabel will be created. But where is that label added to any view? How would anyone see that label?


I just checked and the outlet connection from the file's owner to the actual uilabel in the view is fine.
The evidence presented so far seems to suggest otherwise.

What evidence can you post that the outlet connection is correct?


And when posting code, please use CODE tags.
 
If your outlet connection is setup correctly then also create your property for the label:

Code:
@property (nonatomic, retain) IBOutlet UILabel *infoLabel;
 
That's the wrong solution.

The correct solution is to have a property that can be set from outside the class and which sets a string ivar. Then in viewDidLoad the value of the string ivar is used to set the label. This way will work in the case of a memory warning.

Using a view as the sole repository of application state is a violation of MVC.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.