开发者

Rotating iPhone and changing views

I've never played around with the iPhone being landscape, but I have a fully working iPhone app that is all assuming the user is viewing the iPhone upright. I'd like to play around with rotating the phone, and in order to do that, I did something very simple:

I added the following code to my View Controller:

-(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
    NSLog(@"WILL ROTATE TO INTERFACE ORIENTATION: %@",orientation);

    if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight))
        tab开发者_如何学编程leView.frame = CGRectMake(0, 0, 480, 320);
    else
        tableView.frame = CGRectMake(0,73,320,390);


}

and I'm getting a BAD_ACCESS error. I don't see the view load at all. So, what's the problem, and how do I properly implement the willRotate method so that when the phone rotates, I can resize all my components?


The crash is caused by this line:

NSLog(@"WILL ROTATE TO INTERFACE ORIENTATION: %@",orientation);

orientation is an enum value, therefore the format specifier should be %i not %@.

The simplest way to support rotating is to implement:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

Return YES for the orientations you wish to support. Do not doing any resizing in this method. Use the views struts and springs to ensure that they resize and reposition themselves correctly for the alternate rotations.


It's a problem with your logging code:

NSLog(@"WILL ROTATE TO INTERFACE ORIENTATION: %@",orientation);

The formatting specifier %@ in your logging string is a placeholder for an NSString pointer. When the app reaches this point, it looks at the orientation variable, attempts to dereference it to get to an NSString instance, and crashes, because it's not a pointer at all, it's an enum value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜