查看标签栏应用中未更新

时间:2011-01-29 16:11:15

标签: iphone cocoa-touch uiviewcontroller uitabbarcontroller

我一直在制作一个tabbar应用程序,它有一个让我难过的错误(仍然)。

当我进入新选项卡时,视图不会使用存储在应用程序模型部分中的数据,从而显示陈旧信息(图形,数字等)。我尝试了两种强制更新的方法,在[self updateDisplay]视图中处理。 (1)在app delegate中,使用UITabBarControllerDelegate方法确定选择了哪个选项卡,然后向其发送更新消息。这是委托方法的一部分:

if (viewController == [tabBarController.viewControllers objectAtIndex:1]) {
    NSLog(@"graphViewController selected, graphViewController = %@", graphViewController);
    [graphViewController updateDisplay];                    
}

这就是我想做这项工作的方式。但是,虽然NSLog确认updateDisplay已被触发,但显示不会更新。

(2)按视图中的按钮,执行[self updateDisplay]更新显示。这有效,但并不令人满意。有趣的来了。在下面的日志中,我们读取(1)调用委托方法,(2)选择graphViewController,以及(3)调用graphViewController的updateDisplay方法。大!但此时barGraph为空,并且不更新包含从ourLog中的浮点数读取的标签的显示。对象ourLog是模型的一部分,它与“磁盘”上的文件同步。 (barGraph已从ourLog通过updateDisplay更新。

接下来,我们现在处于由GraphViewController管理的视图中,我们按下“test”按钮(6),调用updateDisplay,视图已正确更新。

请注意,在1--5中,GraphViewController位于0x4e346e0,而在6--9位于0x4b53c80。如果你来回切换,跳出和按下“测试”按钮,你总是得到十六进制数字。起初我以为有两个GraphViewController浮动的实例。但我重写了该类的init方法以便记录它。它只出现一次。所以我完全难过了。

- 吉姆

  • 1] UITabBarControllerDelegate方法,名为self =
  • 2]选中graphViewController,graphViewController =
  • 3] GraphViewController,updateDisplay被调用,self =
  • 4] ourLog:
  • 5] updateDisplay,barGraph:(null)
  • 6]测试推送
  • 7] GraphViewController,updateDisplay被调用,self =
  • 8] ourLog:
  • 9] updateDisplay,barGraph:>

PS。我最初是在UIVIew subclass object becomes mysteriously nil in tabbar app发布的。我正在重新发布这个问题,因为问题更普遍,barGraph在应用程序生命周期的各个点都为空。当我调用它时,某些东西需要可以访问,所以我必须找出一种不同的方式来调用它。但它并没有消失(我也覆盖dealloc并且没有发现barGraph曾被释放过。)

附录1:BarGraph。

以下是在GraphViewController中创建barGraph的方法:

@interface GraphViewController : UIViewController {

BarGraph *barGraph;

}

@property (nonatomic, retain) BarGraph *barGraph;

@property (nonatomic, retain) IBOutlet UIButton *testButton;

- (void) updateDisplay;
- (IBAction) testAction: (id) sender;

@end

@implementationviewDidLoad alloc-inits barGraph:

- (void) viewDidLoad {
    [super viewDidLoad];
    NSLog(@"xx:BARGRAPH: alloc-init in viewDidLoad, GraphBar");
    barGraph = [[BarGraph alloc] initWithFrame:CGRectMake(15, 140, 289, 130)];
    [self.view addSubview:barGraph];    
    [self updateDisplay];
}

最后,这是updateDisplay的实现:

- (void) updateDisplay {

    // Check to see where GraphViewController is:
    NSLog(@"GraphViewController, updateDisplay called, self = %@", self);

    // Get a pointer to the current data to graph
    Log *ourLog = [self theDelegate].currentLog

    // Log info
    NSLog(@"ourLog: %@", ourLog);
    NSLog(@"updateDisplay, barGraph: %@", self.barGraph);

    // Get the current data out of ourLOg
    self.barGraph.data = ourLog.log;
    // And ask barGraph to display it
    [barGraph setNeedsDisplay];
}

附录2:viewWillAppear。

我将viewWillAppear添加到GraphViewController.m,如下所示:

- (void) viewWillAppear:(BOOL)animated
{
     NSLog(@"xx: GraphViewController, viewWillAppear has fired");
     [super viewWillAppear:animated];
     [self updateDisplay];
}

这似乎是正常和最佳的解决方案,但这viewWillAppear永远不会触发。请注意方法testAction,该方法由按钮激活:

- (IBAction) testAction: (id) sender {
    NSLog(@"test pushed");
    [self updateDisplay];
}

按下已连接的按钮以正确更新显示屏。但我当然希望iOS按下按钮,而不是我: - )

1 个答案:

答案 0 :(得分:1)

管理该选项卡的UIViewController应该引用您的自定义UIView。在你的UIViewController的-viewWillAppear方法中,你可以调用-updateDisplay。我假设barGraph是对自定义视图的引用。如果您在xib中创建它,只需确保将它连接到您的IBOutlet,并确保该出口的@property设置为保留。如果您没有在xib中创建barGraph,那么我假设您在viewDidLoad中创建了它。无论哪种方式,请确保将其设置为@property。

如果您没有以任何方式创建它,那么您是如何创建它的?

相关问题