iOS:NIB 之间自动旋转
我的通用应用程序是单个全屏视图。按下按钮会翻转以显示设置页面:
- (void) showSettings
{
FlipsideViewController * flipsideVC = [FlipsideViewController alloc];
NSString * settingsNib;
if ( isIPad() )
settingsNib = isCurrentlyPortrait() ? @"settings_iPad_portrait" : @"settings_iPad_landscape";
else
settingsNib = @"settings_iPhone";
[flipsideVC initWithNibName: settingsNib
bundle: nil ];
flipsideVC.delegatePointingToMainVC = self;
flipsideVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: flipsideVC
animated: YES ];
[flipsideVC release];
}
设置页面会调用委托方法:我根据更改的设置重新创建主视图,然后翻转回来。
- (void) settingsDidQuit:(FlipsideViewController *) flipsideVC
{
[self createOrRecreateWheelView];
[self dismissModalViewControllerAnimated: YES];
}
但如果用户在设置页面旋转 iPad 会怎样呢?苹果规定我的应用程序必须处理这个问题。但如何做到这一点呢?我可以为设置页面动态加载新的 XIB 吗?
我看不到一种方法可以做到这一点,所以我尝试的解决方案是捕获设置视图中的旋转,
- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) oldInterfaceOrientation
{
[self.delegatePointingToMainVC settingsOrientationChanged];
}
......并回调到主视图控制器,它会溶解设置视图控制器并在中重新创建它根据当前方向的光。
- (void) settingsOrientationChanged
{
[self dismissModalViewControllerAnimated: YES];
[self showSettings];
}
马上就会出现一个小问题——当设置页面加载时,didRotateFromInterfaceOrientation 会自动触发。我可以通过在 init 中将布尔值设置为 false 并进行修改来防止这种情况:
- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) oldInterfaceOrientation
{
if (initialized)
[self.delegatePointingToMainVC settingsOrientationChanged];
initialized = true;
}
这种方法的问题是我导航到设置页面,旋转设备,它会立即显示正确的设置页面,然后再弹回到我的主视图。
我认为这里存在线程问题。但也许我的整个方法都是错误的。有人可以提出更好的解决方案吗?
My universal app is a single full screen view. Pressing a button flips to reveal a settings page:
- (void) showSettings
{
FlipsideViewController * flipsideVC = [FlipsideViewController alloc];
NSString * settingsNib;
if ( isIPad() )
settingsNib = isCurrentlyPortrait() ? @"settings_iPad_portrait" : @"settings_iPad_landscape";
else
settingsNib = @"settings_iPhone";
[flipsideVC initWithNibName: settingsNib
bundle: nil ];
flipsideVC.delegatePointingToMainVC = self;
flipsideVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: flipsideVC
animated: YES ];
[flipsideVC release];
}
and the settings page invokes the delegate method: I recreate the main view in light of the changed settings, and flip back.
- (void) settingsDidQuit:(FlipsideViewController *) flipsideVC
{
[self createOrRecreateWheelView];
[self dismissModalViewControllerAnimated: YES];
}
But what if the user rotates the iPad on the settings page? Apple decrees that my app must handle this. But how to do this? can I dynamically load a new XIB for the settings page?
I can't see a way to do that, so my attempted solution is to catch the rotation within the settings view, ...
- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) oldInterfaceOrientation
{
[self.delegatePointingToMainVC settingsOrientationChanged];
}
...and call back to the main view controller, which dissolves the settings view controller and recreates it in light of the current orientation.
- (void) settingsOrientationChanged
{
[self dismissModalViewControllerAnimated: YES];
[self showSettings];
}
There is a trivial problem straight away -- didRotateFromInterfaceOrientation gets triggered automatically when the settings page loads. I can prevent this by setting a boolean to false in init, and modifying thus:
- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) oldInterfaceOrientation
{
if (initialized)
[self.delegatePointingToMainVC settingsOrientationChanged];
initialized = true;
}
problem with this approach is that I navigate to the settings page, rotate the device, and it momentarily shows the correct settings page, before flicking back to my main view.
I think there is a threading problem here. But maybe my whole approach is wrong. Can somebody suggest a better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否理解这个问题。您希望设置视图(从 NIB 加载)自动旋转吗?您应该在 shouldAutorotateToInterfaceOrientation: 中为您希望执行自动旋转的方向返回 YES,并根据您的需要设置 XIB 内视图的自动调整大小掩码。
无需回调主视图控制器并告诉他推送新的设置视图控制器。视图的旋转行为由每个视图的自动调整大小遮罩属性和关联视图控制器的shouldAutorotateToInterfaceOrientation: 方法的实现决定,仅此而已。不过,如果想要执行更高级的动画,您可以在 willRotateToInterfaceOrientation:duration: 和 didRotateFromInterfaceOrientation: 方法中设置和管理它们。
I'm not sure I understand the problem. You want the settings view (loaded from NIB) to autorotate? You should just return YES for the orientation you want the autorotation to be performed in the shouldAutorotateToInterfaceOrientation: and set the autoresizing mask of the views inside the XIB accordingly to your needs.
There's no need to call back the main view controller and tell him to push a new settings view controller. The rotation behavior of the views is determined by the autoresizing mask properties of each view and the implementation of shouldAutorotateToInterfaceOrientation: method of the associated view controller and just that. If want to do more advanced animations, though, you can set up and manage them in the willRotateToInterfaceOrientation:duration: and didRotateFromInterfaceOrientation: methods.