0

I have an Objective-C class that is a subclass of UIViewController. This isn't my main view, but it is accessed when a button is pushed from ViewController through segue in StoryBoard. They are both attached to a navigation controller so they switch around fine. However, I have a problem. In my "DisplayView.m" file (the second view), I make some calculations and want to set the text of a label. I know that there aren't any problems with the calculation portion of the code, because when I NSLog the string, it comes out fine. However, the label does not change. This is my setup for DisplayView scene in Storyboard:

  • Custom class of "DisplayView" set
  • Label that is linked to IBOutlet

Here's my imp file for DisplayView.m if that helps. Please let me know if you need any additional details. Things that I've tried:

  • Creating a new method to print the text
  • Logging the output that is supposed to show up, to the console (works perfect)
  • Trying a different label
  • Trying to use the label objects without @property
  • Trying to setText a non formatted string

None of these tests really worked, and I can't seem to find what the problem is. The new view simply loads without the label. However, if I change the label text in StoryBoard, it does show up. It just isn't showing up any of the values from my code.

DisplayView.m

#import "DisplayView.h"

@interface DisplayView ()

@end

@implementation DisplayView
@synthesize months,days,seconds;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)calc:(int)m with:(int)d andWith:(int)y {

    NSString* bday = [NSString stringWithFormat:@"%i-%i-%i 00:00:00 +0000",m,d,y];
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"LL-dd-yyyy 00:00:00 +0000"];
    NSDate *date1 = [formatter dateFromString:bday];
    NSDate *date2 = [NSDate date];
    NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
    int numberOfDays = secondsBetween/86400;
    [days setText:@"asdadas"];
}


@end

DisplayView.h

#import <UIKit/UIKit.h>

@interface DisplayView : UIViewController {

    IBOutlet UILabel* months;
    IBOutlet UILabel* days;
    IBOutlet UILabel* seconds;


}


-(void)calc:(int)m with:(int)d andWith:(int)y;

@property UILabel* months;
@property UILabel* days;
@property UILabel* seconds;

@end
Carpetfizz
  • 8,707
  • 22
  • 85
  • 146
  • Is days the outlet to the label? Did you log days to see if it's nil? – rdelmar May 19 '13 at 04:55
  • Side note - you want `MM`, not `LL` for the month in the date format. – rmaddy May 19 '13 at 04:57
  • @rdelmar sorry for not mentioning it before, but yes "days" is the outlet to the labe. How would I log to see if it's nil? Also, rmaddy thanks! Just changed it. Main issue still persists though. – Carpetfizz May 19 '13 at 04:59
  • There is a better way to create an `NSDate` from month, day, and year values using `NSDateComponents`. See http://stackoverflow.com/questions/7664786/generate-nsdate-from-day-month-and-year – rmaddy May 19 '13 at 05:00
  • @rmaddy thanks, I'll check it out. However, I'd like to resolve this version of it first. – Carpetfizz May 19 '13 at 05:04
  • @rdelmar Thanks! I actually am getting: (null) as a result of clicking the button. What does this mean and how can I fix it? – Carpetfizz May 19 '13 at 05:04
  • It means you didn't hook up the IBOutlet properly. I can't tell you how to fix it, since I don't know what you did. – rdelmar May 19 '13 at 05:05
  • Hm thanks again, can you tell anything from my updated first post? All I've done in storyboard is wire up the label to this outlet. – Carpetfizz May 19 '13 at 05:09
  • In your .h file are there circles with a black dot in the middle next to the properties that are IBOutlets (to the left of the line numbers)? – rdelmar May 19 '13 at 05:14
  • @rdelmar Yep! To the ones I've hooked up of course, which in this case is just the "days" label. – Carpetfizz May 19 '13 at 05:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30197/discussion-between-rdelmar-and-carpetfizz) – rdelmar May 19 '13 at 05:17
  • Where you are initializing the DisplayView? Can you post the portion of code? – Augustine P A May 19 '13 at 07:35

0 Answers0