【问题标题】:View not drawing when using singleton pattern使用单例模式时视图不绘制
【发布时间】:2013-03-27 14:36:41
【问题描述】:

我在 navigationController 中绘制 UIView,即使用户按下后退按钮,我也需要查看该视图,因此我创建了在 AppDelegate 中创建视图的方法,以便在应用程序之间共享它。

当我使用(AppDelegate *)[[UIApplication sharedApplication] delegate];,然后使用像[appDelegate createBottomView]; 这样的创建视图的方法时,我可以看到视图,但显然如果用户返回然后前进,AppDelegate 将再次实例化它,并且值视图将再次为空。 所以我做了一个单例来在我的类中创建 AppDelegate:

AppDelehate.m
+(id)sharedInstance
{
static dispatch_once_t pred;
static AppDelegate *sharedInstance = nil;
dispatch_once(&pred, ^{
    sharedInstance = [[AppDelegate alloc] init];
    NSLog(@"DISPATCHED");
});
return sharedInstance;
}

我在我的课堂上称它为AppDelegate appDelegate = [AppDelegate sharedInstance];

但是,如果我调用 [appDelegate createBottomView]; 方法,则视图不会在我的导航控制器中绘制。

为什么当我使用单例模式时视图没有被绘制? 我该怎么做?

【问题讨论】:

  • 您的“sharedInstance”看起来不错,但您在“createBottomView”中做什么?

标签: ios delegates uinavigationcontroller singleton


【解决方案1】:
  • 将视图作为 AppDelegate 中的属性
  • 在 AppDelegate 类的 didFinishLaunching 中创建视图
  • 只要您想显示视图,就将视图添加到 viewcontrollers 视图中。

【讨论】:

    猜你喜欢
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    相关资源
    最近更新 更多