UILabel text not updating

Discussion in 'iOS Programming' started by 2Cheap2Switch, Aug 27, 2010.

  1. 2Cheap2Switch macrumors member

    Joined:
    May 16, 2005
    Location:
    Stanford
    #1
    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.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Imagine infoLabel is nil. Describe what would happen with your code?

    How would you obtain evidence that infoLabel isn't nil?
     
  3. 2Cheap2Switch thread starter macrumors member

    Joined:
    May 16, 2005
    Location:
    Stanford
    #3
    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.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    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.


    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
     
  5. 2Cheap2Switch thread starter macrumors member

    Joined:
    May 16, 2005
    Location:
    Stanford
    #5
    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.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    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?


    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.
     
  7. donaghy macrumors member

    Joined:
    Aug 6, 2009
    #7
    If your outlet connection is setup correctly then also create your property for the label:

    Code:
    @property (nonatomic, retain) IBOutlet UILabel *infoLabel;
    
     
  8. 2Cheap2Switch thread starter macrumors member

    Joined:
    May 16, 2005
    Location:
    Stanford
    #8
    I solved it by changing the text after the view was actually pushed onto the navigation stack.
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    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.
     

Share This Page