xcode 4.2选项卡式应用程序Heriarcy

时间:2011-10-25 09:38:10

标签: xcode views flow tabbed

我一直在阅读,我试图找出如何为我的应用程序创建一个正确的视图层次结构。我使用arc在xcode 4.2中创建了一个基本的Tabbed应用程序,但是这个新应用程序不包含mainwindow.xib,因此很多使用IB的教程对我来说都是无用的。我已经有一个5选项卡应用程序工作,应用程序的“基础”按预期工作。我需要的是在当前正在查看的特定“选项卡”窗口中包含更多视图。我的heirarchy很简单,如下所示。我的推测是添加了问号,因为我只有5个基类,所有当前在tabBarController中的UIViewControllers

App heriarchy(建议)

Home (Currently UIViewController using homeViewController, convert to UINavigationController?)
    Add (UIViewController with HomeAdd.xib?)
    Edit (UIViewController with HomeEdit.xib
    delete  (UIViewController with HomeDelete.xib?)

ViewList - Single page to view list (Currently UIViewController using viewListViewController)

Share - Single page to send emails / invites (Currently UIViewController using shareViewController)

Friends (UINavigationController with root view of FriendsRoot.xib?)
    Add (UIViewController with FriendsAdd.xib?)
    Edit (UIViewController with FriendsEdit.xib?)
    delete  (UIViewController with FriendsDelete.xib?)

Settings - Single page for app settings/preferences (Currently UIViewController using settingsViewController)

我甚至不确定上面的层次结构是否可行(即在tabBarController中混合视图控制器),并且喜欢输入如何以编程方式完成此操作,因为这似乎是我在app委托中唯一的选项,因为它有默认情况下没有mainwondiw.h / m / xib。

我在app appate中的当前代码如下:

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) UITabBarController *tabBarController;

@end

AppDelegate.m

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController *homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
    UIViewController *viewListViewController = [[ViewListViewController alloc] initWithNibName:@"ViewListViewController" bundle:nil];
    UIViewController *shareViewController = [[ShareViewController alloc] initWithNibName:@"ShareViewController" bundle:nil];
    UIViewController *friendsViewController = [[FriendsViewController alloc] initWithNibName:@"FriendsViewController" bundle:nil];
    UIViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
    self.tabBarController = [[UITabBarController alloc] init];
    [self.tabBarController setDelegate:self];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:homeViewController, viewListViewController, shareViewController, friendsViewController, settingsViewController, nil];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

我尝试过几次尝试注入UINavigationControllers,或者使用自定义按钮替换视图,但由于我仍然在学习上下文和代码,所有这些都会导致构建失败。

任何建议都会被推荐。到目前为止,我以前的帖子都取得了有限的成功。

谢谢,

Silver Tiger

2 个答案:

答案 0 :(得分:1)

Home(目前使用homeViewController的UIViewController,转换为UINavigationController?) 你可以使用uiviewcontroller作为家庭

对于以下3你可以使用模型视图控制器或你可以添加单独的视图。

添加(带有HomeAdd.xib的UIViewController?)     编辑(带有HomeEdit.xib的UIViewController     删除(带有HomeDelete.xib的UIViewController?)

朋友的情况也是如此..

我不确定为什么你找不到mainwindow.xib ..

答案 1 :(得分:0)

坏消息是,由于较新的Tab应用程序默认没有“MainWindow.xib”,因此您无法在线查看大量教程。好消息是它仍然很容易实现。

中的AppDelegate.m文件中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

方法(你设置tabBarController的视图控制器,你需要实际添加类型为“UINavigationController”的ViewControllers而不是类型“UIViewController”(你需要创建这些文件)。

例如(为了缩写,我只展示了我的最后一个UINavigationController声明),

UINavigationController *nodes = [[NodeNavigationController alloc] initWithNibName:@"NodeNavigationController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:modems, nodes, home,viewController1, viewController2, nil];

最后,在你的UINavigationController的init方法(或类似的)

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

你需要添加这个......

Nodes *viewController = [[Nodes alloc] initWithNibName:@"Nodes" bundle:[NSBundle mainBundle]];
    self.viewControllers = [NSArray arrayWithObjects:viewController, nil];

现在在“Nodes”viewController中你可以使用你一直使用的常用“[... pushViewController:...]”东西。

...示例

[self.navigationController pushViewController:viewController animated:YES];

我希望这很有帮助。