iOS:NIB 之间自动旋转

发布于 2024-09-25 01:41:19 字数 1820 浏览 3 评论 0原文

我的通用应用程序是单个全屏视图。按下按钮会翻转以显示设置页面:

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

又爬满兰若 2024-10-02 01:41:19

我不确定我是否理解这个问题。您希望设置视图(从 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文