我遇到了标签问题,未在我的用户界面中正确更新。我有一个使用大纲视图切换视图的Mac OS X应用程序。我只想在切换到(FirstViewController)的视图上的标签上显示给用户的日期。当在一个新项目中单独实施时,我没有问题。但是,如果在视图更改的地方实现,标签的值不会更新,事实上,即使先前设置了控制台输出,也表明_dateLabel为(null)。有什么建议么?我必须错过一些非常基础的东西!标签更新为空
控制台输出:
2014年8月30日19:54:22.719 OutlineView [10420:1613022] StringedText是2014年8月30
2014年8月30日19:54:22.720 OutlineView [10420: 1613022]标签值(NULL)
我包括下面的代码:
//
// FirstViewContorller.h
// OutlineView
#import <Cocoa/Cocoa.h>
@interface FirstViewContorller : NSViewController
@property (weak) IBOutlet NSTextField *dateLabel;
-(void)updateDateScreen;
@end
//
// FirstViewContorller.m
// OutlineView
#import "FirstViewContorller.h"
@implementation FirstViewContorller
@synthesize dateLabel = _dateLabel;
-(void)updateDateScreen{
//date calculation for main window
NSDate *now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
NSString *stringedText = [formatter stringFromDate:now];
_dateLabel.stringValue = stringedText;
NSLog(@"StringedText is %@", stringedText);
NSLog(@"label value is %@", _dateLabel.value);
}
@end
//
// AppDelegate.m
// OutlineView
#import "AppDelegate.h"
#import "Book.h"
#import "FirstViewContorller.h"
@interface AppDelegate()
@property (weak) IBOutlet NSOutlineView *outlineView;
@property (weak) IBOutlet NSTreeController *booksController;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
//other code here
//Call the update method in FirstViewController to load date label
FirstViewContorller *instance = [[FirstViewContorller alloc]init];
[instance updateDateScreen];
}
//further unrelated code
@end
您必须等到viewDidLoad或viewWillAppear才能更新UI对象,因为它们在视图加载之前不存在。而且您还需要确保UI对象正确链接到XIB中的类。 – 2014-08-30 13:25:43