gpt4 book ai didi

xcode - 在 AppDelegate 之外使用 NIB 创建 UITabBarController?

转载 作者:行者123 更新时间:2023-12-02 00:25:46 24 4
gpt4 key购买 nike

我还是 iOS 编程的新手,尽管进行了大量研究,但我还是遇到了另一个障碍。

我要实现的:

我想要一个在我从主 UI 导航时加载的 UITabBarController。我还想使用 NIB 来定义它的属性。

我能找到的所有示例都将 UITabBarController 放在 AppDelegate 中,但我不想加载它,除非它被使用。我也不知道如果我只是模态地执行它,是否所有的 UIGestureRecognizers 都会保持事件状态(我无法获得有效的实现)。

到目前为止我有什么

首先,我从 AppDelegate 加载一个初始加载 View

AppDelegate.h

@class InitialViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;

@end

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[InitialViewController alloc] initWithNibName:@"InitialViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

从这个角度来看,因为我只是制作 UI 的框架,所以我有两个按钮,一个转到主界面,另一个转到 UITabBarController。

InitialViewController.h

@interface InitialViewController : UIViewController
- (IBAction)toMain:(id)sender;
- (IBAction)toTabs:(id)sender;

@property (strong, nonatomic) UIViewController *mviewController;
@property (strong, nonatomic) UIViewController *tviewController;
@end

InitialViewController.m

- (IBAction)toMain:(id)sender {

self.mviewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
[[[UIApplication sharedApplication] delegate] window].rootViewController = self.mviewController;

}

- (IBAction)toTabs:(id)sender {
self.tviewController = [[tabViewController alloc] initWithNibName:@"tabViewController" bundle:nil];

[[[UIApplication sharedApplication] delegate] window].rootViewController = self.tviewController;

}

在加载 MainViewController 时,它的行为与我想要的完全一样。但是当我加载选项卡 View 时,我会在底部看到一个长选项卡和黑色背景。我可以在 viewdidload 中添加内容,例如更改背景颜色,但没有实际的选项卡或 View 链接到 XIB 中的选项卡。

我怀疑我在两个方面缺少某些东西:在选项卡 .h 中,以及在界面生成器中与此相关联的一些链接。或者设置一个新的 rootViewController 是不够的。

tabBarController.h

#import <UIKit/UIKit.h>

@interface iPodViewController : UITabBarController <UITabBarControllerDelegate>
@end

如果有人能为我指出正确的方向和/或向我展示一个可行的实现方案,我将不胜感激。

-- 请注意,当我进入 tabbar.xib 并使用辅助编辑器时,它会打开 InitialViewController.h --

最佳答案

与其他 View Controller (例如 UITableViewController)不同,您不应该继承 UITabViewController。因此,与您其他的 View Controller 不同,您不进行子类化,然后让您的子类成为 nib 的所有者,指向 nib 中的 View ,具有自定义 View 。

相反,对于您想拥有 UITabBarController 的任何类,添加一个普通的普通 UITabBarController 作为此类的导出属性。 (例如,您的应用委托(delegate))。

然后创建一个nib文件,将一个UITabBarController对象拖入nib中。将 nib 的所有者设置为您想要拥有标签栏 Controller 的类(例如,您的应用委托(delegate)),并将您作为属性创建的导出连接到 nib 中的标签栏 Controller 。

@interface myTabOwningClass

@property (strong, nonatomic) IBOutlet UITabBarController myTabBarControllerOutlet;

现在,在您要创建和显示标签栏 Controller 的位置,使用以下方法:

[[NSBundle mainBundle] loadNibNamed:@"MyTabControllerNib" owner:myTabOwningClass options:nil];

这将初始化所属类的属性(即我们示例中的 myTabBarControllerOutlet),并从 nib 加载选项卡栏 Controller ,包括您在 nib 中定义的每个选项卡的所有 subview Controller 等。

关于xcode - 在 AppDelegate 之外使用 NIB 创建 UITabBarController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8978488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com