开发者

iPhone -- structuring code to avoid a race condition

I have an iPhone app that uses a UINavigationController, some table views, and iAd. At the top level, my navigation controller shows the navigation bar. At lower levels, it does not.

The problem I am having is that sometimes the frame of my top level UITableView goes below the bottom of the screen. The reason it happens is this:

my viewWillAppear method looks like this:

-(void) viewWillAppear:(BOOL)animated开发者_高级运维 {
    [self.navigationController setNavigationBarHidden:NO animated: animated]; // changing the last animated to NO does not help.
    [super viewWillAppear:animated];
}

and my viewDidLoad method looks like this:

- (void)viewDidLoad {
[self.navigationController setNavigationBarHidden:NO animated: NO];
    [super viewDidLoad];
    [self createTableView];
    ADBannerView *abv = [[ADBannerView alloc]initWithFrame: [self initialBannerViewFrame]];
    abv.delegate=self;
    [self.view addSubview:abv];
    self.bannerView = abv;
    [self moveBannerViewOffscreen];
    [abv release];
}

Lastly, moveBannerViewOffscreen looks like this:

-(void) moveBannerViewOffscreen {
    // moving it down and off
    CGRect newBannerFrame = self.bannerView.frame;
    CGFloat screenHeight = [[UIScreen mainScreen]bounds].size.height;
    newBannerFrame.origin.y=screenHeight;
    bannerView.frame = newBannerFrame;
    CGRect newTableFrame = self.selectionTableView.frame;
    newTableFrame.size.height = self.view.bounds.size.height;
    self.selectionTableView.frame = newTableFrame;
}

When the view is loading, what happens is that even though I have called

[self.navigationController setNavigationBarHidden: NO animated: NO];

the the frame of my view is not immediately adjusted to account for the navigation bar. This is still true when moveBannerViewOffscreen executes. So the height of the table view is set to 480. When the navigation bar comes in, the result is that the bottom of the table view is below the screen, and the user can't select the last row.

I'm sure I could use an NSTimer to set up some kludge to fix this. But is there a clean way to organize my code so the problem doesn't come up in the first place?

Thanks


At first glance (without fully understanding your problem, I admit) I suspect that setting yourself as the navigation controller's delegate in order to take advantage of one of these methods would help with your timing:

navigationController:didShowViewController:animated:

navigationController:willShowViewController:animated:

perhaps not moving your banner until didShowViewController has been called.

(Apologies if I didn't follow your explanation.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜