UITabBarController 的多个视图

发布于 2024-08-26 17:05:02 字数 1075 浏览 4 评论 0原文

我正在尝试构建一个应用程序,其中有一个包含 4 个条目的 TabBarController。 当我选择第一个条目时,会显示一个带有 UITableView 的视图。 该 TableView 充满了多个条目。

我想做的是: 当选择 UITableView 中的一个条目时,应该会显示另一个视图;详细视图。

.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.row == 0) {

if(self.secondView == nil) {
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];
self.secondView = secondViewController;
[secondViewController release];
}

// Setup the animation
[self.navigationController pushViewController:self.secondView animated:YES];

}
}

.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"


@interface FirstViewController : UIViewController {

SecondViewController *secondView;

NSMutableArray *myData; 
}
@property (nonatomic, retain) SecondViewController *secondView;
@property (nonatomic, copy, readwrite) NSMutableArray* myData;

@end

这是我到目前为止所拥有的。

不幸的是..代码运行,但第二个视图没有显示。

I'm trying to build an app where I have a TabBarController with 4 entries.
When I select the first entry, a view with a UITableView shows up.
This TableView is filled with several entries.

What I would like to do is:
When an entry out of that UITableView gets selected, another view should show up; a detailview.

.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.row == 0) {

if(self.secondView == nil) {
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];
self.secondView = secondViewController;
[secondViewController release];
}

// Setup the animation
[self.navigationController pushViewController:self.secondView animated:YES];

}
}

.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"


@interface FirstViewController : UIViewController {

SecondViewController *secondView;

NSMutableArray *myData; 
}
@property (nonatomic, retain) SecondViewController *secondView;
@property (nonatomic, copy, readwrite) NSMutableArray* myData;

@end

This is what I have so far.

Unfortunately.. the code runs, bit the second view does not show up.

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

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

发布评论

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

评论(1

¢好甜 2024-09-02 17:05:02

您的第一个视图控制器是否包含在 UINavigationController 中?当您设置UITabBarController时,您应该添加UINavigationControllers而不是UIViewController子类,例如:

FirstViewController *viewControl1 = [[FirstViewController alloc] init];
UINavigationController *navControl1 = [[UINavigationController alloc] initWithRootViewController:viewControl1];
UITabBarController *tabControl = [[UITabBarController alloc] init];
tabControl.viewControllers = [NSArray arrayWithObjects:navControl1, <etc>, nil];
//release everything except tabControl

此外,根据您的代码,您不需要将secondViewController保留为ivar,因为UINavigationController会自动保留它的视图控制器(当你不显示它时保留它会消耗不必要的内存)。

Is your first view controller wrapped in a UINavigationController? When you set up your UITabBarController, you should add UINavigationControllers rather than your UIViewController subclasses, e.g.:

FirstViewController *viewControl1 = [[FirstViewController alloc] init];
UINavigationController *navControl1 = [[UINavigationController alloc] initWithRootViewController:viewControl1];
UITabBarController *tabControl = [[UITabBarController alloc] init];
tabControl.viewControllers = [NSArray arrayWithObjects:navControl1, <etc>, nil];
//release everything except tabControl

Also, based on your code, you don't need to keep your secondViewController as an ivar, since UINavigationController automatically retains its view controllers (and retaining it when you're not displaying it will use up unnecessary memory).

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