Ios UIC标签不更新XCode

Ios UIC标签不更新XCode,ios,objective-c,uilabel,Ios,Objective C,Uilabel,我对iOS开发非常陌生。我开发了一个代码,但它没有更新任何标签。我做错了什么? 代码如下。我正在使用故事板,导航正常工作,但无法更新UILabel // TodayScheduleViewController.m @interface TodayScheduleViewController () @end @implementation TodayScheduleViewController @synthesize timeNowLabel; @synthesize timeNow;

我对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

通过使用断点,我能够追溯dayOfWeek标签未更新的原因:

[dateComps weekday]返回的最大值是NSInteger可以在一周中的某一天使用的最大值。这是因为您以错误的方式初始化了NSDateComponents*dateComps:

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComps = [calendar components:(NSWeekdayCalendarUnit) fromDate:[NSDate date]];
在你的情况下,这是正确的方法


现在,请尝试使用断点回溯其他标签没有更新的原因,因为您没有为我提供足够的代码进行更新。

感谢大家的支持。 我可以更新为

    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];

再次感谢。

在故事板中,标签是否链接为IBOutlet?是的,我已通过按住ctrl键并拖动到头文件中的方式将其附加。hm。。。我认为所提供的代码没有任何问题。IBOutlet最好是弱属性,而NSString应该定义为copy。我上传了一个基于你的代码,一切正常。非常感谢,我会试试这个。嗨,hoiberg42,谢谢其他标签正在工作,但它说日历是未声明的,我确实忘记了声明日历。我会更新我的答案。