TodayWidget viewDidLoad没有运行

时间:2015-03-12 21:10:13

标签: ios objective-c ios8 viewdidload ios8-today-widget

我最近在我的应用程序中添加了一个Widget,或者已经尝试过。当我运行它时,小部件显示在通知中心,但它显示为折叠。我已经确认没有使用NSLog调用viewDidLoad。任何想法为什么不加载?感谢。

编辑:似乎问题是标签不会被添加我必须在界面构建器中创建所有内容。

.h文件

#import <UIKit/UIKit.h>
#import <NotificationCenter/NotificationCenter.h>

@interface TodayViewController : UIViewController {
    UILabel *lblCurrentLocation;

}
#define SCREEN ([[UIScreen mainScreen] bounds])

@end

.m文件

#import "TodayViewController.h"

@interface TodayViewController () <NCWidgetProviding>

@end

@implementation TodayViewController

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    lblCurrentLocation = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    lblCurrentLocation.text = @"Current Location: Unknown";
    [self.view addSubview:lblCurrentLocation];
    NSLog(@"Set up");

}


- (void)widgetPerformUpdateWithCompletionHandler:(void (^)    (NCUpdateResult))completionHandler {
// Perform any setup necessary in order to update the view.

// If an error is encountered, use NCUpdateResultFailed
// If there's no update required, use NCUpdateResultNoData
// If there's an update, use NCUpdateResultNewData

completionHandler(NCUpdateResultNewData);
}

@end

3 个答案:

答案 0 :(得分:0)

也发生在我身上(很多)。据我所知,在调试小部件时有很多故障排除。

请尝试以下步骤:

  1. 停止运行小部件。
  2. 点击通知中心底部的“修改”按钮,从通知中心的今日视图中删除小部件。
  3. 关闭通知中心。
  4. 再次运行小部件。

答案 1 :(得分:0)

请检查类名称 TodayViewController 分配给您的主界面构建器。

答案 2 :(得分:-1)

对于“今日”分机,通常不会每次都呼叫viewDidLoad。但是,viewWillAppearviewDidAppear每次都会被调用。要加载新数据,应将代码放入此方法:

func widgetPerformUpdate(completionHandler: ((NCUpdateResult) -> Void))

通常,在今天扩展的情况下,我们使用上述方法代替viewDidLoad来获取和设置数据。每当需要新数据时都将调用此方法。(通常每次调用,但是如果您频繁切换NotificationCenter,则仅在第一次调用此方法)。因此,根据您的需要使用所需的方法。

有关更多详细信息,请参阅:https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/Today.html

相关问题