Search code examples
iosobjective-cuilabeliboutlet

Setting UILabel text is not working


Here is my .h file

#import <UIKit/UIKit.h>

@interface PersonViewController : UIViewController

@property(strong,nonatomic) NSString *personTitle;

And here is my .m file

@interface PersonViewController ()

@property (weak, nonatomic) IBOutlet UILabel *titleView;

@end

@implementation PersonViewController
//stuff …

-(void)setPersonTitle:(NSString *)personTitle
{
    [self.titleView setText:personTitle];// also self.titleView.text=personTitle
    [self.titleView setNeedsDisplay];
    NSLog(@"The title shoud match as %@ :: %@",personTitle,self.titleView.text);
}

-(NSString *)personTitle
{
    return self.titleView.text;
}

//… more stuff

@end

The logging shows that the value is (null) for self.titleView.text whereas personTitle prints the appropriate value.

I remember doing this same thing a number of times and it worked. Any ideas why it’s failing this time?

update I use storyboard to set my scenes. And I am using xcode-5 and iOS-7

update: how I call

The user clicks a button, leading to a push segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"enter prepare for segue.");
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];

 if ([segue.identifier isEqualToString:the_identifier_for_person]) {
        NSLog(@"segue to person is progressing“);
        if ([segue.destinationViewController isKindOfClass:[PersonViewController class]]) {
            NSLog(@"segue to person destination is a match");

            PersonViewController *aPerson = (PersonViewController *)segue.destinationViewController;
            aPerson.personTitle=((MyItem*)self.allItems[indexPath.row]).title;
            NSLog(@"segue to person is done");

        }
    }
}

Solution

  • Do you actually call the -(void)setPersonTitle:(NSString *)personTitle method?

    It seems that you aren't calling it correctly which would result in the title being null.

    After reviewing the prepareForSeque it is clear that you are not calling the method. You are actually just changing the @property named personTitle.

    In the viewDidLoad you should have it so that self.titleView.text = self.personTitle;