Reputation: 163
I have a few buttons on my View and when the app starts they should flip around their x-axis. When I start the app from Xcode (Build and then run the current scheme) they flip, but when I open the app on my iPhone they don't flip.
My viewDidLoad in my ViewController looks like this:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
model =[[Model alloc] init];
GameView *gameView = [[GameView alloc] initWithTiles];
gameView.controller = self;
self.view = gameView;
[self flipButtons];
}
Why don't the buttons flip when I start the app from my phone?
If you need additional information just let me know :)
Edit
This is the method that flips the button and gets called for each button:
- (void)flip:(Color*)color {
int flipDirection = arc4random() % 2;
if(flipDirection == 0) flipDirection = -1;
double duration = 200 + arc4random() % (500 - 200);
duration = duration / 1000;
// Animation
[UIView animateWithDuration:duration
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
self.layer.transform = CATransform3DMakeRotation(M_PI, 0, flipDirection, 0);
// wait duration /2 and call changeColor
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((duration / 2) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self changeColor:color];
});
}
completion:^(BOOL finished) {
self.layer.transform = CATransform3DMakeRotation(M_PI, 0, 0, 0);
}];
}
Upvotes: 0
Views: 84
Reputation: 9311
viewDidLoad
doesn't necessarily mean that the view is on-screen. You should be using viewWillAppear:
or viewDidAppear
:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
model =[[Model alloc] init];
GameView *gameView = [[GameView alloc] initWithTiles];
gameView.controller = self;
self.view = gameView;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self flipButtons];
}
viewDidLoad
only signifies that the view controller's view has been instantiated and by that point all IBOutlet
s have been wired up. You should only do additional initialization here, and must not rely on when viewDidLoad
was called or whether it was called multiple times, etc (e.g. you can unload your view controller's view with self.view = nil
if it doesn't have a superview
, and it'll be automatically reloaded when it's needed).
Why this is necessary:
UIViewController
loads it's view
lazily. When you instantiate a UIViewController
with e.g initWithNibName:bundle:
it won't load the nib right away; that would be a waste of resources. According to Apple's documentation:
The nib file you specify is not loaded right away. It is loaded the first time the view controller's view is accessed. If you want to perform additional initialization after the nib file is loaded, override the viewDidLoad method and perform your tasks there.
So what happens is, when the view controller's view
is queried the first time, loading takes place (see: Order of UIViewController initialization and loading ) and the viewDidLoad
call is issued so that you can make adjustments to the view with all the subviews and IBOutlets
set up.
Once the view is loaded, the view controller's state transitions are described in this diagram:
The initial state is disappeared
.
Upvotes: 2