0

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

应用层次结构(建议)

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 中混合视图控制器),并且希望输入有关如何以编程方式完成此操作的输入,因为这似乎是我在应用程序委托中的唯一选择,因为没有 mainwondiw。默认为 h/m/xib。

我在我的应用程序委托中的当前代码如下:

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,或使用自定义按钮替换视图,但似乎都导致构建失败,因为我仍在学习上下文和代码。

任何建议都会受到赞赏。到目前为止,我以前的帖子取得的成功有限。

谢谢,

银虎

4

2 回答 2

1

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

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

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

同样的情况也适用于朋友..

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

于 2011-10-26T10:27:27.600 回答
0

坏消息是,由于较新的选项卡应用程序默认没有“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”视图控制器中,您可以使用通常使用的“[...pushViewController:...]”内容。

例子...

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

我希望这可以帮到你。

于 2011-11-15T21:00:24.460 回答