推送视图控制器 - viewDidAppear 未调用

发布于 2024-12-14 01:27:51 字数 1727 浏览 7 评论 0原文

我有这段代码来推送视图控制器:

        // Setup the animation
[self.navigationController pushViewController:self.productView animated:YES];

self.productView.imageURL = [product imageURL];

// Set the title of the view to the product's name
self.productView.title = [product name];

// Set the label text of all the labels in the view
[self.productView.caloriesL setText:[product calories]];
[self.productView.fatL setText:[product fat]];
[self.productView.saturatesL setText:[product saturates]];
[self.productView.sugarL setText:[product sugar]];
[self.productView.fibreL setText:[product fibre]];
[self.productView.saltL setText:[product salt]];

但是当productView出现时,委托方法viewDidAppear不会被调用。我在谷歌上查找了这个问题,有很多不同的解决方案,但没有一个可以应用于我的问题。我在之前的解决方案中遇到了类似的问题,但我通过在 viewDidLoad 方法中手动调用 viewDidApear 来解决它。不幸的是,在这种情况下我不能这样做,因为 viewDidLoad 仅被调用一次(在第一次推送时)。有谁知道如何解决这个问题?

谢谢,

Jack Nutkins

编辑:

这是productView(和选择器)中的viewDidAppear 方法:

- (void)viewDidAppear:(BOOL)animated{
//Start animating the activity indicator
[indicator startAnimating];
//Perform this method in background
[self performSelectorInBackground:@selector(loadImage) withObject:nil];

}

- (void) loadImage {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // Load the animals image into a NSData boject and then assign it to the UIImageView
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
    UIImage *image = [[UIImage alloc] initWithData:imageData];
    self.imageView.image = image;

    //Stop animating the activity indicator
    [indicator stopAnimating];

    [pool drain]; //see comment below
}

I have this piece of code to push a view controller:

        // Setup the animation
[self.navigationController pushViewController:self.productView animated:YES];

self.productView.imageURL = [product imageURL];

// Set the title of the view to the product's name
self.productView.title = [product name];

// Set the label text of all the labels in the view
[self.productView.caloriesL setText:[product calories]];
[self.productView.fatL setText:[product fat]];
[self.productView.saturatesL setText:[product saturates]];
[self.productView.sugarL setText:[product sugar]];
[self.productView.fibreL setText:[product fibre]];
[self.productView.saltL setText:[product salt]];

But the delegate method viewDidAppear does not get called when the productView appears. I looked up the problem on google and theres a lot of different solutions, none of which I could apply to my problem.. I had a similar problem in a previous solution but I got around it by manually calling viewDidApear in the viewDidLoad method. Unfortunately in this case I can't do that as viewDidLoad is called only once (on the first push). Does anyone know how to fix this?

Thanks,

Jack Nutkins

EDIT:

Here is the viewDidAppear method in the productView (and selector):

- (void)viewDidAppear:(BOOL)animated{
//Start animating the activity indicator
[indicator startAnimating];
//Perform this method in background
[self performSelectorInBackground:@selector(loadImage) withObject:nil];

}

- (void) loadImage {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // Load the animals image into a NSData boject and then assign it to the UIImageView
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
    UIImage *image = [[UIImage alloc] initWithData:imageData];
    self.imageView.image = image;

    //Stop animating the activity indicator
    [indicator stopAnimating];

    [pool drain]; //see comment below
}

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

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

发布评论

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

评论(2

記柔刀 2024-12-21 01:27:51

首先:您绝对不想调用任何标准的 viewWillLoadviewDidLoadviewWillAppear 等. 手动方法。让操作系统为您做这件事。

第二:您能否向我们展示您的 viewDidAppear 方法是如何在您的 self.productView 实例中实现的? (只是预感,您不希望在导航控制器上调用此方法,对吧?)我只是想确保您的方法签名完全正确。如果不是(由于拼写错误、参数不正确等),那么它肯定不会被调用。

第三:我会将您的 pushViewController: 调用移至您提供的其余代码之后。您不希望将视图推送到屏幕上(以便用户可以看到它),然后屏幕上的一堆值立即发生变化。首先设置您的 ivars 和 title 属性,然后推送视图控制器。这消除了任何奇怪的闪烁。

First: You definitely don't want to be calling any of the standard viewWillLoad, viewDidLoad, viewWillAppear, etc. methods manually. Let the OS do it for you.

Second: Can you show us how your viewDidAppear method is implemented in your self.productView instance? (Just a hunch, you're not expecting this method to be called on your navigation controller, right?) I just want to make sure your method signature is exactly correct. If it's not (due to a mispelling, improper args, etc.) then it definitely won't be called.

Third: I would move your pushViewController: call to after the rest of the code you provided. You don't want the view to be pushed on the screen (so the user can see it) and then have a bunch of on-screen values immediately change. Set your ivars and title property first, then push the view controller. This eliminates any weird flickering.

夏の忆 2024-12-21 01:27:51

我解决了它,虽然它看起来并不传统,但不敢相信我没有早些时候尝试过:

我把这一行:

[self.productView viewDidAppear:YES];

放在下面:

    // Setup the animation
[self.navigationController pushViewController:self.productView animated:YES];

我还移动了代码以将标签文本设置为在上一行之前运行。 (以及更改我的代码以将字符串发送到推送的控制器而不是访问其 UI 元素。)

感谢大家的帮助,

Jack

I solved it, though it doesn't seem conventional, can't believe I didn't try it earlier :

I put this line :

[self.productView viewDidAppear:YES];

Underneath :

    // Setup the animation
[self.navigationController pushViewController:self.productView animated:YES];

I also moved the code to set the labels text to run before the above line. (As well as changing my code to send strings to the pushed controller rather that accessing its UI elements.)

Thanks for everyones help,

Jack

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