iPhone:使用带有 Tabview 控制器和导航控制器的单例
我使用单例开发了一个小型 iPhone 应用程序,我用它来浏览视图。这是我的单例类中的示例方法。
+ (void) loadMenuController:(NSMutableArray *)menuItems{
MenuViewController *menuViewControler = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
[menuViewControler setMenuItems:menuItems];
RootViewController *root = (
P2MAppDelegate *appDelegate = (P2MAppDelegate*) [[UIApplication sharedApplication] delegate];
UINavigationController *navController = [appDelegate navigationController];
[navController pushViewController:menuViewControler animated:YES];
[menuViewControler release];
}
现在我的要求已更改为需要选项卡视图控制器。我可以将应用程序委托更改为选项卡视图控制器,但我仍然需要在每个选项卡内导航。我无法找到如何从我的单例类中导航的线索。
请指导我。如果我的疑问不清楚,请告诉我。
提前致谢。
问候, 马勒斯瓦尔
I have developed a small iPhone application by using singleton that I use to navigate through the views. Here is a sample method from my singleton class.
+ (void) loadMenuController:(NSMutableArray *)menuItems{
MenuViewController *menuViewControler = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
[menuViewControler setMenuItems:menuItems];
RootViewController *root = (
P2MAppDelegate *appDelegate = (P2MAppDelegate*) [[UIApplication sharedApplication] delegate];
UINavigationController *navController = [appDelegate navigationController];
[navController pushViewController:menuViewControler animated:YES];
[menuViewControler release];
}
Now my requirement has changed to require a tab view controller . I could change my application delegate to a tabview controller but I still need to navigate inside each tab. I am unable get a clue how to navigate from my singleton class.
Please guide me. Please let me know if my query is not clear.
Thanks in advance.
Regards,
Malleswar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该使用单例来管理界面,即使您这样做,您也不会将 UI 逻辑放在类方法中。您需要从头开始重新考虑您的设计。
正常模式是将导航控制器或选项卡栏控制器作为应用程序委托的属性。应用程序委托本身不应该是任何控制器的子类,而只是实现应用程序委托协议的 NSObject 子类。
查看 Apple 在 Xcode 中提供的模板项目,了解围绕导航和/或选项卡构建应用程序的快速而肮脏的方法。
仅当您必须确保类的一个且只有一个实例同时处于活动状态时,才应使用单例。您不需要创建自己的单例来管理 UI。应用程序委托附加到应用程序对象,该对象本身就是一个单例。这意味着应用程序委托为您可能需要的 UI 类提供了所有限制。除此之外,您不需要另一个单例。
过度使用单例是危险的,可能会导致您的设计陷入死胡同,从而导致大规模重写。在雇用他们之前请仔细考虑。
You shouldn't be using a singleton to manage the interface and even if you did, you wouldn't put the UI logic in a class method. You need to rethink your design from scratch.
The normal pattern is to hold the navigation controller or the tabbar controller as an attribute of the application delegate. The app delegate itself should not be a subclass of any controller but just a NSObject subclass that implements the application delegate protocol.
Look at the Apple supplied template projects in Xcode to see the quick and dirty way to structure apps built around navigation and/or tabs.
Singletons should only be used when you have to ensure that one and only one instance of class is alive at one time. You don't need to make your own singleton to manage the UI. The application delegate is attached to the application object which is itself a singleton. This means the app delegate provides all the restriction on class for the UI you might need. You don't need another singleton in addition to that.
Overuse of singletons is dangerous and can cause your design to get trapped in a dead end resulting in a massive rewrite. Think carefully before employing them.