我应该将 UITabBarController 放在 App Delegate 之外吗?

发布于 2024-08-21 17:06:42 字数 1093 浏览 3 评论 0原文

我遵循“开始 iPhone 3 开发”中的示例,该示例将主视图控制器(选项卡栏)的代码放在委托方法中。这是放置此文件的正确位置还是应该将其放在单独的 .h 和 .m 文件中?我的所有子视图都位于单独的文件中,因此我想知道是否应该将选项卡栏视图控制器代码也放在单独的文件中。

另外,对于子视图,我照常调用 ViewDidLoad,但委托方法中没有 ViewDidLoad,我猜是因为它的类型是 NSObject 而不是 UIViewController。我应该将委托更改为 UIViewController 类型以便我可以调用 ViewDidLoad 吗?

谢谢,我现有应用程序的代码示例如下。

Delegate 的头文件:

#import <UIKit/UIKit.h>

@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

    UIWindow *window;
    UITabBarController *rootController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *rootController;

@end

Delegate 实现文件的开头

#import "MyAppDelegate.h"


@implementation MyAppDelegate

@synthesize window;
@synthesize rootController;    

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    // Add the tab bar controller's current view as a subview of the window
    [window addSubview:rootController.view];
    [window makeKeyAndVisible];
}

I followed an example from "Beginning iPhone 3 Development" which puts the code for the main view controller, a Tab Bar, in the delegate method. Is this the correct place to put this or should it be in a separate .h and .m file? All my subviews are in separate files so I'm wondering if I should have my tab bar view controller code in a separate file also.

Also, for the subviews I call ViewDidLoad as normal but there is no ViewDidLoad in the delegate method, I guess because it's of type NSObject and not UIViewController. Should I change the delegate to a type UIViewController so I can call ViewDidLoad?

Thanks, code samples of my existing app are below.

Header file for Delegate:

#import <UIKit/UIKit.h>

@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

    UIWindow *window;
    UITabBarController *rootController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *rootController;

@end

Beginning of Delegate implementation file

#import "MyAppDelegate.h"


@implementation MyAppDelegate

@synthesize window;
@synthesize rootController;    

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    // Add the tab bar controller's current view as a subview of the window
    [window addSubview:rootController.view];
    [window makeKeyAndVisible];
}

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

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

发布评论

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

评论(1

慵挽 2024-08-28 17:06:42

这是放置此文件的正确位置还是应该将其放在单独的 .h 和 .m 文件中?
我应该将委托更改为 UIViewController 类型,以便我可以调用 ViewDidLoad 吗?

不,这是您的初始加载点,而不是视图控制器。即使您更改其类型,视图的加载方法也不会被调用,应用程序委托不是视图控制器。您可以在此处加载初始视图控制器。 UITabbar(根据 doco)“这个类不适合子类化。”请参阅此处。 (所以没有 .h 和 .m 文件,你会从什么派生?)你不需要子类化,因为你将为你放在标签栏中的每个视图获取 viewdidload 方法。

Is this the correct place to put this or should it be in a separate .h and .m file?
Should I change the delegate to a type UIViewController so I can call ViewDidLoad?

no this is your initial load point, not a view controller. Even if you change its type, the view did load method will not be called, the app delegate is not a view controller. It is here you load your initial view controller. UITabbar (according to the doco) "This class is not intended for subclassing." see here. (so no .h and .m file, what would you derive from?) you should not need to subclass, as you will get your viewdidload method for each of the views you put in your tab bar.

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