Question

This has been driving me nuts all day! I am a relative beginner at objective-c, so excuse my mistakes!

What I want to do is assign some buttons a random x position on the screen on a view, this is the code I'm using:

int screenwidth = ([UIScreen mainScreen].bounds.size.height - 100);
[_Game0_Button1 setCenter:(CGPointMake(arc4random_uniform(screenwidth) + 50, _Game0_Button1.center.y))];

While this works fine, when a subview loads or unloads the new positions that are assigned are reset back to their original positions, such as:

[self addChildViewController: GamePausedVC];

GamePausedVC.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
GamePausedVC.delegate = self;
[self.view addSubview: GamePausedVC.view];
[GamePausedVC didMoveToParentViewController: self];

[GamePausedVC willMoveToParentViewController:nil];
[GamePausedVC.view removeFromSuperview];
[GamePausedVC removeFromParentViewController];

What am I doing wrong?

Thanks!

Was it helpful?

Solution

Keep an array of your random positions and reset the button positions in viewWillAppear.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top