使用单例模式时查​​看不绘图

时间:2013-03-27 14:36:42

标签: ios delegates uinavigationcontroller singleton

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

当我使用(AppDelegate *)[[UIApplication sharedApplication] delegate];然后使用创建View的方法[appDelegate createBottomView];时,我可以看到视图,但显然如果用户返回然后转发,AppDelegate将再次实例化,并且视图的值将再次为null。 所以我创建了一个单例来在我的类中创建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];,则视图不会在我的navController中绘制。

为什么在使用单例模式时未绘制视图? 我该怎么办?

1 个答案:

答案 0 :(得分:0)

  • 将视图作为AppDelegate中的属性
  • 在AppDelegate类的didFinishLaunching中创建视图
  • 只要您想要显示视图,就可以将视图添加到viewcontrollers视图。
相关问题