我对 iOS 很陌生,在为 iOS8 编写 hello world Today 扩展时遇到了 2 个问题。我尝试使用模板创建一个简单的 Today 扩展,效果很好。
但是,如果我尝试了以下其中一项,则该小部件不会出现在 Today 应用程序中(只有我的扩展程序的标题,但没有正文):
删除模板自带的“hello world”标签,新增1个带有“hello world 2”的标签;我注意到如果我这样做,约束也会自动删除。
在today view controller的storyboard中取消勾选“Use Auto Layout”,创建UILabel的属性并链接到storyboard中的label,然后在viewDidLoad的label上调用setText:
TodayViewController.m:
@interface TodayViewController () <NCWidgetProviding>
@property (nonatomic, weak) IBOutlet UILabel *cityLabel;
@end
@implementation TodayViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self _updateTodayView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
// Perform any setup necessary in order to update the view.
[self _updateTodayView];
// If an error is encountered, use NCUpdateResultFailed
// If there's no update required, use NCUpdateResultNoData
// If there's an update, use NCUpdateResultNewData
completionHandler(NCUpdateResultNewData);
}
- (void) _updateTodayView {
[[self cityLabel] setText:@"Hello World 3"];
}
@end
我多年来一直在 Windows 上进行开发,我很惭愧被这个问题阻止了一整天。
我的环境:
OS X Yosemite。Xcode 6.1