在 iPhone 上用一个视图调用选项卡式视图的最佳方法
编辑让我简化问题:假设您的应用程序有 两个视图,主视图和选项卡视图 具有 3 个选项卡的视图。主视图有 三个名为“Tab1”的标准按钮, “选项卡2”和“选项卡3”。当您点击 “Tab1”,选项卡式视图应该是 打开并选择选项卡 1 Tab2 和 Tab3 也是如此。
有什么提示吗?
/编辑
我想要做的事情:当我的 iPhone 应用程序初始化时,它会显示一个带有一个按钮的视图。当我单击此按钮时,它应该进入选项卡式视图。
我正在做的方式,但它不起作用(出现未捕获的异常):我首先从基于视图的模板创建一个项目。在自动创建的视图控制器上,我声明了另一个名为 TabbedViewController 的视图控制器。这是头文件:
#import <UIKit/UIKit.h>
@class TabbedViewController;
@interface DZBluePagesViewController : UIViewController {
TabbedViewController *tabbedViewController;
}
@property (nonatomic, retain) TabbedViewController *tabbedViewController;
-(IBAction)goToTabbedView:(id)sender;
@end
我创建了一个名为 TabbedView
的 xib,并将文件所有者的类标识设置为 TabbedViewController。我还将一个选项卡栏控制器拖到了它上面。
我在主视图上添加了一个按钮,这是我在主视图控制器文件上与其关联的操作:
- (void)viewDidLoad {
TabbedViewController *tvc = [[TabbedViewController alloc]
initWithNibName:@"TabbedView" bundle:nil];
self.tabbedViewController = tvc;
[tvc release];
[super viewDidLoad];
}
-(IBAction)goToTabbedView:(id)sender {
// [self.view removeFromSuperview];
[self.view insertSubview:tabbedViewController.view atIndex:0];
}
我在 insertSubview 调用上遇到了未捕获的异常...我已经完成了 3 或 4 个教程(摘自《iPhone 开发 - Mark 和 LaMarche - 书》)到目前为止,我一直在谈论多视图应用程序,我以为我已经准备好在没有人牵着我手的情况下做一个了……显然是错误的。有人可以帮忙吗?
EDIT Let me simplify the question: Suppose your application has
two views, the home view and a tabbed
view with 3 tabs. The home view has
three standard buttons named "Tab1",
"Tab2" and "Tab3". When you click on
"Tab1", the tabbed view should be
opened with tab 1 selected and the
same goes for Tab2 and Tab3.
Any hints?
/EDIT
What I'm trying to do: when my iPhone app initializes, it displays a view with one button. When I click this button, it should go to a tabbed view.
The way I'm doing and it's not working (getting an uncaught exception): I started by creating a project from the view-based template. On the view controller that was auto-created, I've declared another view controller called TabbedViewController. Here's the header file:
#import <UIKit/UIKit.h>
@class TabbedViewController;
@interface DZBluePagesViewController : UIViewController {
TabbedViewController *tabbedViewController;
}
@property (nonatomic, retain) TabbedViewController *tabbedViewController;
-(IBAction)goToTabbedView:(id)sender;
@end
I've created a xib called TabbedView
and set the class identity of the file owner to the TabbedViewController. I've also dragged a Tab Bar Controller to it.
I've added a button on the home view and here's the action that I've associated with it on the main view controller file:
- (void)viewDidLoad {
TabbedViewController *tvc = [[TabbedViewController alloc]
initWithNibName:@"TabbedView" bundle:nil];
self.tabbedViewController = tvc;
[tvc release];
[super viewDidLoad];
}
-(IBAction)goToTabbedView:(id)sender {
// [self.view removeFromSuperview];
[self.view insertSubview:tabbedViewController.view atIndex:0];
}
I'm getting an uncaught exception on the insertSubview call... I've done 3 or 4 tutorials (from the iPhone Development - Mark and LaMarche - book) on multiview apps so far and I thought I was ready to do one without anyone holding my hands... obviously wrong. Anybody can help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了回答您的简化问题,
我认为您需要第三个“根”视图和视图控制器作为主视图和选项卡栏视图的超级视图。
当您的应用程序启动时,根视图将加载主页视图的内容。您可以使用以下几行在根视图控制器内执行此操作:
然后,当您想要切换到选项卡栏视图时,您可以这样做:
有一个问题:除非您调用,否则不会调用 viewWillAppear:、viewDidAppear: 方法它们是你自己的,所以你也必须将它们插入到正确的位置。
按照 Ben Gottlieb 的建议,使用 UINavigationController 作为根视图可能会更容易。(我的意思是 root 作为我一直在使用它的感觉,导航控制器的根视图将是你的家视图。)然后,您可以调用pushViewController:方法来将tabBarViewController滑入,而不是上面的代码。如果您这样做,您将需要自定义UINavigationController来隐藏导航栏,因为您可能不想要它,而且苹果实际上不建议在导航视图层次结构中放置标签栏。
关于异常
您发布的代码似乎与您发布的异常没有任何关系。 请参阅此博客文章以获取有关以下内容的说明:在抛出的异常上设置全局断点。这样您就可以使用调试器来找出您自己的程序中有问题的代码行。
To answer your simplified question
I think you need a third "root" view and view controller to be the superview of both the Home view and then the Tab Bar view.
When your app starts, the Root view will load the contents of the Home view. You can do so inside the root view controller with these lines:
Then, when you want to switch to the tab bar view, you would do this:
There's a catch: the viewWillAppear:, viewDidAppear: methods won't be called unless you call them yourself, so you'll have to insert those in the right places, too.
It might be easier to do as Ben Gottlieb suggests, and use a UINavigationController as the root view. (I mean root as the sense I've been using it, the navigation controller's root view will be your home view.) Then, instead of the code above, you can just call the pushViewController: method to slide the tabBarViewController in. If you do this you will want to customize the UINavigationController to hide the navigation bar, since you probably don't want it, and Apple actually does not recommend putting a tab bar inside a navigation view hierarchy.
Regarding the exception
The code you posted doesn't seem to have anything to do with the exception you posted. See this blog post for instructions on setting a global breakpoint on thrown exceptions. That way you can use the debugger to figure out what the offending line of code in your own program is.
出现异常时打开运行控制台(shift-command-R)。发布那个。您可能还想打开控制台,然后在提示符下键入“bt”作为“回溯”。
发布异常。
Open the run console (shift-command-R) when you get the exception. Post that. You may also want to open the console and, at the prompt, type "bt" for "back trace".
Post the exception.
我建议使用 UINavigationController,然后在他们选择按钮后将 tabController 推到上面。如果您不需要幻灯片动画,只需为animated:参数传递NO即可。
I would suggest using a UINavigationController, and then pushing your tabController onto it once they select a button. If you don't want the slide animation, just pass NO for the animated: argument.
为什么有多余的菜单视图?为什么不将选项卡视图作为自己的视图并让用户选择他们想要选择的选项卡?我不明白为什么你需要一个只打开特定选项卡的菜单,而选项卡栏界面无论如何都不需要菜单...
Why have a redundant menu view? Why not just have the tab view as a view on its own and let the user select which tab they wish to choose? I don't understand why you need a menu that just opens a particular tab when the tab bar interface does that anyway without the menu...