iPhone——构建代码以避免竞争条件

发布于 2024-10-07 10:31:12 字数 1682 浏览 0 评论 0原文

我有一个 iPhone 应用程序,它使用 UINavigationController、一些表格视图和 iAd。在顶层,我的导航控制器显示导航栏。在较低的水平上,则不然。

我遇到的问题是,有时我的顶级 UITableView 的框架会低于屏幕底部。发生这种情况的原因是这样的:

我的 viewWillAppear 方法看起来像这样:

-(void) viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO animated: animated]; // changing the last animated to NO does not help.
    [super viewWillAppear:animated];
}

我的 viewDidLoad 方法看起来像这样:

- (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];
}

最后, moveBannerViewOffscreen 看起来像这样:

-(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;
}

当视图加载时,会发生什么即使我已经调用了

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

视图的框架不会立即调整以适应导航栏。当 moveBannerViewOffscreen 执行时仍然如此。所以表视图的高度设置为480。当导航栏进来时,结果是表视图的底部低于屏幕,用户无法选择最后一行。

我确信我可以使用 NSTimer 设置一些杂凑来解决这个问题。但是有没有一种干净的方法来组织我的代码,这样问题就不会首先出现呢?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花期渐远 2024-10-14 10:31:12

乍一看(我承认,在没有完全理解您的问题的情况下)我怀疑将自己设置为导航控制器的委托以利用其中一种方法将有助于您的计时:

navigationController:didShowViewController:animated:

navigationController:willShowViewController:animated:

也许直到 didShowViewController< 才移动您的横幅/code> 已被调用。

(如果我没有听从你的解释,我很抱歉。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文