views:

24

answers:

1

Hi

I have one main view where I display an image, in the method viewDidLoad:

ballRect = CGRectMake(posBallX, 144, 32.0f, 32.0f); theBall = [[UIImageView alloc] initWithFrame:ballRect]; [theBall setImage:[UIImage imageNamed:@"ball.png"]]; [self.view addSubview:theBall]; [laPalla release];

Obviously, the value of posBallX is defined and then update via a custom method call many times in the same class.

theBall.frame = CGRectMake(posBallX, 144, 32, 32);

Everything works, but when I go to another view with

[self presentModalViewController:viewTwo animated:YES];

and come back with

[self presentModalViewController:viewOne animated:YES];

the image is displayed correctly after the method viewDidLoad is called (I retrieve the values with NSUserDefaults) but no more in the second method. In the NSLog I can even see the new posBallX updating correctly, but the Image is simply no more shown... The same happens with a Label as well, which should print the value of posBallX.

So, things are just not working if I come back to the viewOne from the viewTwo... Any idea???????

Thanks so much!

A: 

You should use dismissModalViewControllerAnimated: to switch back to viewOne from viewTwo instead of trying to present viewOne modally.

Also note that viewDidLoad is called only once - after the view controller's view is loaded into memory. If you want to perform an action once a view comes back on screen, you should do so in viewWillAppear:.

Both of these points are discussed in the UIViewController class reference and in the View Controller Programming Guide.

Gordon Hughes
Thanks! Today I give your solution a try and let you know. You were very very kind! However... I'm wondering why the dismissModalViewControllerAnimated should work and the one I use not. I will read again the docs. Using [self presentModalViewController:viewOne animated:YES] to go back to the first, viewDidLoad is called again. But more logical is viewWillAppear method... I let you know! Thanks!
Fabio Ricci
When you show viewOne modally the second time, you've got two copies of viewOne in the view hierarchy. `viewDidLoad` is called because you've allocated and initialised the view controller a second time. You just need to dismiss viewTwo to return to the original viewOne.
Gordon Hughes