子类化? UITabBarController 不会自动旋转

发布于 2024-11-06 18:21:59 字数 2953 浏览 0 评论 0原文

菜鸟请耐心听我说。

我一直在关注 O'Rielyy 学习 iPhone 编程和这里的各种主题来构建我的第一个 iPhone 应用程序。到目前为止一切顺利,但项目结束时的最后一个绊脚石是让应用程序自动旋转(仅使用 uiwebviews 的测试版因不自动旋转而被拒绝)

我有邮件应用程序委托,它添加了一个 UITabBarController

// myNewsUKDelegate.h
@interface myNewsUKDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
    UIWindow *window;
    UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end

// myNewsUKDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Add the tab bar controller's view to the window and display.
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

有 .h tabBarController 的 .m 和 .m 文件 - 我在 IB 中添加了所有 UINavigationControllers,这又添加了一个 UITableView

请参阅 http://flatearth.co.uk/nib.png (太菜鸟了,无法在问题中发布图片!)

从我的阅读中,我了解到问题是我添加到主视图的 UITabBarController 需要“子类化”并添加此代码。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

下一个视图向下/进入/子类化(无论正确的术语是什么),它具有 .h 和 .m 文件,它是添加表视图的 FirstViewController,它已经设置了 shouldAutorotateToInterfaceOrientation。

@interface FirstViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
    UITableView *tableView;
    NSArray *userList;
}
@property (nonatomic, retain) IBOutlet UITableView *tableView;
@property (nonatomic, retain) NSArray *userList;
@end

@implementation FirstViewController

@synthesize tableView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // I tried adding
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    // lots of other code ; )
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

所以问题似乎是当 [self.window addSubview:tabBarController.view]; 时添加标签栏,但不添加返回 YES 位的 shouldAutorotateToInterfaceOrientation。

看来我需要添加一个 tabBarController 子类,其中包含 shouldAutorotateToInterfaceOrientation 。所以我阅读并尝试了这个,按照互联网上的建议...

//  tabBarController.h
#import <UIKit/UIKit.h>
@interface tabBarController : UITabBarController {

}
@end

//  tabBarController.m
#import "tabBarController.h"
@implementation tabBarController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
@end

并添加

#import "tabBarController.h"

到 myNewsUKDelegate.m

但失败,并在

[self.window addSubview:tabBarController.view];

myNewsUKDelegate.m 中的行

显示“错误:访问未知的“视图”类方法”进一步搜索尚未产生任何有用的东西和我最近的 Xcode 知识现在已经枯竭了:(任何帮助表示赞赏。

Noobie so bear with me.

I've have been following the O'Rielyy Learning iPhone Programming and various threads on here to build my first iPhone App. So far so good, but the final stumbling block at the projects end is getting the App to autorotate (the beta using only uiwebviews was rejected for not auto-rotating)

I have the mail App delegate, which adds a UITabBarController

// myNewsUKDelegate.h
@interface myNewsUKDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
    UIWindow *window;
    UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end

// myNewsUKDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Add the tab bar controller's view to the window and display.
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

There is are .h and .m files for tabBarController - I added all the UINavigationControllers in IB, which in turn add a UITableView

See image at http://flatearth.co.uk/nib.png (too noob to post images in questions!)

From my reading I understand that the issue is the UITabBarController I added to the main view needs to be 'subclassed' and have this code added.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

The next view down/in/subclassed (whatever the correct terminology is), which has .h and .m files is the FirstViewController which adds the table view, this has shouldAutorotateToInterfaceOrientation already set.

@interface FirstViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
    UITableView *tableView;
    NSArray *userList;
}
@property (nonatomic, retain) IBOutlet UITableView *tableView;
@property (nonatomic, retain) NSArray *userList;
@end

@implementation FirstViewController

@synthesize tableView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // I tried adding
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    // lots of other code ; )
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

So the problem appears to be that when [self.window addSubview:tabBarController.view]; adds the tab bar it doesn't add the shouldAutorotateToInterfaceOrientation returning YES bit.

It appears that I need to add a tabBarController subclass, with the shouldAutorotateToInterfaceOrientation in it. So I read up and tried this, as suggested on the interwebs...

//  tabBarController.h
#import <UIKit/UIKit.h>
@interface tabBarController : UITabBarController {

}
@end

//  tabBarController.m
#import "tabBarController.h"
@implementation tabBarController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
@end

and adding

#import "tabBarController.h"

to myNewsUKDelegate.m

But that fails with "error: accessing unknown 'view' class method" at the

[self.window addSubview:tabBarController.view];

line in myNewsUKDelegate.m

Further searching hasn't produced anything helpful and my recent Xcode knowledge has now ran dry : ( Any help appreciated.

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

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

发布评论

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

评论(2

谈场末日恋爱 2024-11-13 18:21:59

根据我的阅读,我了解到问题是我添加到主视图的 UITabBarController 需要“子类化”并添加此代码。

不,你不需要这样做。选项卡栏控制器通过询问其所有子控制器是否支持特定界面方向来确定其是否支持特定界面方向。在您的情况下,这些似乎是导航控制器,它们反过来询问当前的子控制器是否支持方向。

换句话说,您必须确保所有自定义视图控制器都返回 YES 以获得所需的界面方向。

From my reading I understand that the issue is the UITabBarController I added to the main view needs to be 'subclassed' and have this code added.

No, you don't need to do that. The tab bar controller determines if it supports a specific interface orientation or not by asking all its child controllers if they support this orientation. In your case, these seem to be navigation controllers, which in turn ask their current child controller if it supports the orientation.

In other words, you have to make sure that all your custom view controllers return YES for the desired interface orientation.

泪冰清 2024-11-13 18:21:59

您不需要子类,您需要 UITabBarController 上的类别。 中创建一个名为 UITabBarController + Autoresize.h (和 .m)的文件,

基本上,您在 .h:

#import <UIKit/UIKit.h>

@interface UITabBarController (Autoresize) 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
@end

和 .m:

#import "UITabBarController + Autoresize.h"

@implementation UITabBarController (Autoresize)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{   
    //do custom checks here if you only want to autorotate it in certain views or whatever
}
@end

但正如另一张海报所指出的,您希望旋转的视图的所有父视图都必须支持旋转。

You don't need a subclass, you need a Category on UITabBarController. Basically you create a file called UITabBarController + Autoresize.h (and .m)

In the .h:

#import <UIKit/UIKit.h>

@interface UITabBarController (Autoresize) 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
@end

in the .m:

#import "UITabBarController + Autoresize.h"

@implementation UITabBarController (Autoresize)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{   
    //do custom checks here if you only want to autorotate it in certain views or whatever
}
@end

but as the other poster pointed out, ALL the parent views of the view you wish to rotate must support rotation.

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