【问题标题】:TodayWidget viewDidLoad not runningTodayWidget viewDidLoad 未运行
【发布时间】:2015-03-12 21:10:12
【问题描述】:

我最近在我的应用程序中添加了一个小部件,或者尝试过添加。当我运行它时,小部件显示在通知中心,但它似乎已折叠。我已经确认没有使用 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

【问题讨论】:

  • 请出示您的代码,以便我们检查。这描述得不好,而且太模糊了。
  • @bolencki13 你有什么解决办法吗?我面临着类似的问题!
  • 我不得不使用界面生成器。我无法以编程方式添加标签或任何东西

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


【解决方案1】:

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

尝试以下步骤:

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

【讨论】:

  • 我得到了它的工作问题是我无法以编程方式添加标签(没有尝试其他任何东西)我最终使用了界面生成器
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    如果是 Today 扩展,viewDidLoad 不是每次都被调用是正常的。但是,viewWillAppearviewDidAppear 每次都会被调用。为了加载新数据,你应该把代码放在这个方法中:

    func widgetPerformUpdate(completionHandler: ((NCUpdateResult) -&gt; Void))

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

    更多详情可以参考:https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/Today.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多