仅更改选项卡应用程序中一个选项卡的视图(横向-纵向)?

发布于 2024-08-20 20:38:42 字数 1298 浏览 4 评论 0原文

旋转 iPhone 时如何更改视图(更改笔尖)。 但它应该只发生在一个选项卡中! 我尝试过:

    - (void)viewDidLoad {
 LandscapeViewController *viewController = [[LandscapeViewController alloc]
             initWithNibName:@"LandscapeView" bundle:nil];
 self.landscapeViewController = viewController;
 [viewController release];

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
             name:UIDeviceOrientationDidChangeNotification object:nil]; }

- (void)orientationChanged:(NSNotification *)notification
{
    [self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}

- (void)updateLandscapeView
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
 {
        [self presentModalViewController:self.landscapeViewController animated:YES];
        isShowingLandscapeView = YES;
    }
 else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
 {
        [self dismissModalViewControllerAnimated:YES];
        isShowingLandscapeView = NO;
    }    
}

但随后横向视图出现在所有选项卡中。 (当此代码加载一次时)。 有什么想法吗?

How can I change the view when rotating the iphone (change nib's).
But it should only happens in one single tab!
I tried it with:

    - (void)viewDidLoad {
 LandscapeViewController *viewController = [[LandscapeViewController alloc]
             initWithNibName:@"LandscapeView" bundle:nil];
 self.landscapeViewController = viewController;
 [viewController release];

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
             name:UIDeviceOrientationDidChangeNotification object:nil]; }

- (void)orientationChanged:(NSNotification *)notification
{
    [self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}

- (void)updateLandscapeView
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
 {
        [self presentModalViewController:self.landscapeViewController animated:YES];
        isShowingLandscapeView = YES;
    }
 else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
 {
        [self dismissModalViewControllerAnimated:YES];
        isShowingLandscapeView = NO;
    }    
}

But then the Landscape-View appears in all Tabs. (When this code is loaded once).
Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

关于从前 2024-08-27 20:38:42

发生的情况是,每当旋转发生变化时,无论是否显示,您的视图控制器都会收到 UIDeviceOrientationDidChangeNotification 通知。相反,请尝试使用 UIViewController 的内置方法来响应旋转。

http://tinyurl.com/ycb8of2

What's happening is your view controller is receiving the UIDeviceOrientationDidChangeNotification notification whenever the rotation changes, whether or not it is being displayed. Instead try using the built-in methods of UIViewController that are meant for responding to rotations.

http://tinyurl.com/ycb8of2

耳钉梦 2024-08-27 20:38:42

这是来自苹果的文档(视图控制器编程指南):

标签栏控制器和视图旋转

标签栏控制器支持纵向
默认方向,不
旋转至横向
除非所有的根视图
控制器支持这样的
方向。当设备方向
发生变化时,标签栏控制器
查询其视图控制器数组。
如果其中任何一项不支持
方向、标签栏
控制器不改变其
方向。

因此,我不确定选项卡栏控制器是否设计为仅针对单个视图进行旋转。

This is from Apple's Docs (View Controller Programming Guide):

Tab Bar Controllers and View Rotation

Tab bar controllers support a portrait
orientation by default and do not
rotate to a landscape orientation
unless all of the root view
controllers support such an
orientation. When a device orientation
change occurs, the tab bar controller
queries its array of view controllers.
If any one of them does not support
the orientation, the tab bar
controller does not change its
orientation.

So, I'm not sure that the Tab Bar Controller is designed to rotate just for a single view.

不甘平庸 2024-08-27 20:38:42

坦克欢迎您的评论!
我找到了一个解决方法:

//Remove Observer if not in Landscape
- (void)viewDidDisappear:(BOOL)animated {
    if (isShowingLandscapeView == NO) {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
    }   
}

//Add Observer if not in Landscape
- (void)viewDidAppear:(BOOL)animated {
    if (isShowingLandscapeView == NO) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
    }
}

Tanks for your comments!
I found a workaround:

//Remove Observer if not in Landscape
- (void)viewDidDisappear:(BOOL)animated {
    if (isShowingLandscapeView == NO) {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
    }   
}

//Add Observer if not in Landscape
- (void)viewDidAppear:(BOOL)animated {
    if (isShowingLandscapeView == NO) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文