结合 UITabController 和UINavigation 控制器以编程方式 http://t.co/R52RlUL UITabController &以编程方式进行 UINavigation 控制器

发布于 2024-10-31 20:35:06 字数 920 浏览 0 评论 0原文

我已经创建了一个 UItabbarcontroller 和 2 个带有 UITableViews 的视图,仅使用代码(没有 IB 的东西),我现在想在顶部添加一个导航栏,其中包括添加和编辑按钮,但是我似乎被绊倒并炸毁了我的应用程序或仅将导航控制器添加到第三个选项卡。

这是我添加选项卡栏和切换视图的主要代码

仅供参考 - 我正在使用 XCode4

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.mainTabBar = [[UITabBarController alloc] init];

// create the 2 views
tableViewController* vc1 = [[tableViewController alloc] init];
tableViewController2* vc2 = [[tableViewController2 alloc] init];

// put them in an array
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];

// for the tab bar
mainTabBar.viewControllers = controllers;

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

// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}

I have created a UItabbarcontroller and 2 views with UITableViews just using code (no IB stuff) and I want to now add a navigation bar at the top that will include add and edit buttons, however I seem to be tripping up and blowing my app up or adding navgation controller to a 3rd tab only.

Here is my main code for adding the tab bar and switching views

FYI - I am using XCode4

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.mainTabBar = [[UITabBarController alloc] init];

// create the 2 views
tableViewController* vc1 = [[tableViewController alloc] init];
tableViewController2* vc2 = [[tableViewController2 alloc] init];

// put them in an array
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];

// for the tab bar
mainTabBar.viewControllers = controllers;

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

// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}

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

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

发布评论

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

评论(1

二手情话 2024-11-07 20:35:06

您想要哪里的导航控制器?您必须为 UITabBarController 中想要的每个选项卡创建一个。

您添加一个 navigationController 及其堆栈上的第一个视图控制器。试试这个:

// create the controllers for UITabBarController
tableViewController *vc1 = [[[TableViewController alloc] init] autorelease];
navController *nav1 = [[[UINavigationController alloc] initWithRootViewController:vc1] autorelease];

tableViewController *vc2 = [[[TableViewController alloc] init] autorelease];
navController *nav2 = [[[UINavigationController alloc] initWithRootViewController:vc2] autorelease];

// put them in an array
NSArray *controllers = [NSArray arrayWithObjects:nav1, nav2, nil];

// rest of your code

另请注意,您需要释放您分配或保留的任何内容。您可以像我一样,在初始化它们时添加 autorelease ,也可以在将它们添加到 controllers 数组后显式释放它们。

然后,您可以在其 loadViewviewDidLoad 方法中为每个视图控制器配置 navigationItem,具体取决于您的实现方式。

Where do you want navigation controller(s)? You have to create one for each tab you want one in the UITabBarController.

You add a navigationController in conjunction with the first view controller on its stack. Try this:

// create the controllers for UITabBarController
tableViewController *vc1 = [[[TableViewController alloc] init] autorelease];
navController *nav1 = [[[UINavigationController alloc] initWithRootViewController:vc1] autorelease];

tableViewController *vc2 = [[[TableViewController alloc] init] autorelease];
navController *nav2 = [[[UINavigationController alloc] initWithRootViewController:vc2] autorelease];

// put them in an array
NSArray *controllers = [NSArray arrayWithObjects:nav1, nav2, nil];

// rest of your code

Also note that you need to release anything you alloc or retain. You can do it as I did by adding autorelease when you initialize them or you can release them explicitly after you've added them to the controllers array.

You then configure the navigationItem for each view controller in its loadView or viewDidLoad method depending on how you implemented it.

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