UILabels没有更新XCode

时间:2014-09-14 04:47:26

标签: ios objective-c uilabel

我是iOS开发的新手。我开发了一个代码,但它没有更新任何标签。我做错了什么?     代码如下。我正在使用故事板,导航工作正常,但我无法更新UILabel。

//  TodayScheduleViewController.m

@interface TodayScheduleViewController ()
@end


@implementation TodayScheduleViewController

@synthesize timeNowLabel;
@synthesize timeNow;
@synthesize lampImage;
@synthesize groupSchedule;
@synthesize lightStatus;
@synthesize timeRemaining;
@synthesize dayOfWeek;

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

#pragma mark - View lifecycle

 -(void)viewDidLoad
{
    [super viewDidLoad];
    // This will pass the values of to UILabel every time the view will load


    NSDate *currentTime = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh-mm"];
    NSString *resultString = [dateFormatter stringFromDate: currentTime];
    self.timenowlabel.text = resultString;

    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    NSInteger weekday = [dateComps weekday];
    if (weekday==1) {dayOfWeek = @"Sunday";}
    else if(weekday==2){dayOfWeek = @"Monday";}
    else if(weekday==3){dayOfWeek = @"Tuesday";}
    else if(weekday==4){dayOfWeek = @"Wednesday";}
    else if(weekday==5){dayOfWeek = @"Thursday";}
    else if(weekday==6){dayOfWeek = @"Friday";}
    else if(weekday==7){dayOfWeek = @"Saturday";}

    self.dayofweek.text = self.dayOfWeek;


    groupSchedule = [NSString stringWithFormat:@"%d", GroupNumber];
    self.groupschedule.text = self.groupSchedule;

    self.lightstatus.text = @"This is it";

}

头文件如下:

@interface TodayScheduleViewController : UIViewController

// These 2 NSString objects are created because it will pass the values to the UILabels
@property (weak, nonatomic) NSString *timeNowLabel;
@property (weak, nonatomic) NSString *timeNow;
@property (weak, nonatomic) NSStream *lampImage;
@property (weak, nonatomic) NSString *groupSchedule;
@property (weak, nonatomic) NSString *lightStatus;
@property (weak, nonatomic) NSString *timeRemaining;
@property (weak, nonatomic) NSString *dayOfWeek;

// These are the outlets of the UILabels that we want to change based on the button selected
@property (strong, nonatomic) IBOutlet UILabel *timenowlabel;
@property (strong, nonatomic) IBOutlet UILabel *time_now;
@property (strong, nonatomic) IBOutlet UIImageView *lampimage;
@property (strong, nonatomic) IBOutlet UILabel *groupschedule;
@property (strong, nonatomic) IBOutlet UILabel *lightstatus;
@property (strong, nonatomic) IBOutlet UILabel *timeremaining;
@property (strong, nonatomic) IBOutlet UILabel *dayofweek;


@end

2 个答案:

答案 0 :(得分:0)

使用断点,我能够回溯dayOfWeek标签没有更新的原因:

[dateComps weekday]返回MAX_INT(NSInteger可以拥有的最大值),而不是星期几。那是因为您错误地初始化了NSDateComponents *dateComps

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComps = [calendar components:(NSWeekdayCalendarUnit) fromDate:[NSDate date]];

这是你的正确方法。

现在尝试使用断点来回溯为什么其他标签没有更新,因为您没有给我足够的代码来执行此操作。

答案 1 :(得分:0)

谢谢大家的支持。 我能够更新为,

    NSDate *currentTime = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *resultString = [dateFormatter stringFromDate: currentTime];
    self.time.text = resultString;

/*    NSDateComponents *dateComps = [[NSDateComponents alloc] init];

    NSInteger weekday = [dateComps weekday];
    if (weekday==1) {self.dayofweek.text = @"Sunday";}
    else if(weekday==2){self.dayofweek.text = @"Monday";}
    else if(weekday==3){self.dayofweek.text = @"Tuesday";}
    else if(weekday==4){self.dayofweek.text = @"Wednesday";}
    else if(weekday==5){self.dayofweek.text = @"Thursday";}
    else if(weekday==6){self.dayofweek.text = @"Friday";}
    else if(weekday==7){self.dayofweek.text = @"Saturday";}*/

    [dateFormatter setDateFormat: @"EEEE"];
    self.dayofweek.text = [dateFormatter stringFromDate:currentTime];

再次感谢你。