多个视图和应用程序代理?

时间:2011-08-15 14:45:01

标签: iphone objective-c

好吧,我显然是iOS开发的新手,所以请耐心等待。我看到在互联网上浮动的大多数教程都基于单一视图模板,但我想将其中一些教程组合到一个标签栏中。我有三个属性列表,我想分配给各个选项卡并用作向下钻取的导航数据。我被告知我可以从应用程序didFinishLaunching方法为各个选项卡分配字典属性,但我不明白如何将它们分配给不同的选项卡。

所以这就是我所拥有的

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

[self.window makeKeyAndVisible];
[self.window addSubview:rootViewController.view];

NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *DataPath = [Path stringByAppendingPathComponent:@"LocationsData.plist"];

NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath];
self.locationsData = tempDict;
[tempDict release];

[self.window addSubview:locNavControl.view];
[window makeKeyandVisible];

NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *DataPath = [Path stringByAppendingPathComponent:@"IndustriesData.plist"];

NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath];
self.industryData = tempDict;
[tempDict release];

[self.window addSubview:indNavControl.view];
[window makeKeyandVisible];

return YES;
}

错误发生在self.window addSubview:locNavControl.view,因为“请求成员'视图'的东西不是结构或联合”。我想我可能有那个部分错了,因为我希望它在按下标签时将数据推到屏幕上。警告在makeKeyandVisible上,因为“window”可能没有响应。

我认为我做错了,但我不知道。一些帮助将不胜感激:)

2 个答案:

答案 0 :(得分:1)

您只能在任何指定时间制作一个视图键!调用[window makeKeyandVisible] 3次不起作用。

修改

您的窗口只能有一个根视图控制器。在你的情况下,它可能是一个标签栏控制器。然后,您可以为不同的选项卡创建单独的控制器,并将它们添加到选项卡栏控制器。

修改2

一般来说,你可能会做这样的事情......

在appdelegate.h文件中

...

UITabBarController *tabBarController;
UIViewController *vc1;
UIViewController *vc2;
//... etc
UIViewController *vcn; 

然后在你的实现文件appdelegate.m中,你应该初始化视图控制器,然后将它们添加到你的tabbarcontroller。

//Your tabBarController should be connected to the user interface (XIB file), so no need to allocate it in code

//here you can choose to allocate vc1 through vcn if they are not also set up in the XIB
//I recommend doing setup like loading from plist files in each view controller's viewDidLoad method

//If you didn't add the viewcontrollers in the XIB you could do something like this...

NSArray *viewControllers = [NSArray arrayWithObjects:vc1, vc2, ..., vcn, nil];
[tabBarController setViewControllers:viewControllers animated:NO];

self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];

这只是一个快速的指导方针,我希望能够解决一些问题!我建议仔细查看文档并浏览一些Apple的示例代码和教程。他们非常有帮助!

答案 1 :(得分:0)

在我看来,您可以使用UITabBarController来实现您提到的基于选项卡的导航。请阅读Apple's UITabBarController documentation以获取更多信息。