【问题标题】:Combining UITabController & UINavigation controller programmatically http://t.co/R52RlUL UITabController & UINavigation controller programmatically以编程方式结合 UITabController 和 UINavigation 控制器 http://t.co/R52RlUL UITabController 和 UINavigation 控制器以编程方式
【发布时间】:2011-04-12 18:33:41
【问题描述】:

我已经创建了一个 UItabbarcontroller 和 2 个带有 UITableViews 的视图,只使用代码(没有 IB 的东西),我现在想在顶部添加一个导航栏,其中包括添加和编辑按钮,但是我似乎正在绊倒和吹我的应用启动或仅将导航控制器添加到第三个选项卡。

这是我添加标签栏和切换视图的主要代码

仅供参考 - 我正在使用 XCode4

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.mainTabBar = [[UITabBarController alloc] init];

// create the 2 views
tableViewController* vc1 = [[tableViewController alloc] init];
tableViewController2* vc2 = [[tableViewController2 alloc] init];

// put them in an array
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];

// for the tab bar
mainTabBar.viewControllers = controllers;

// Add the tab bar controller's current view as a subview of the window
[self.window addSubview:self.mainTabBar.view]; 

// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}

【问题讨论】:

    标签: iphone ios uitableview uinavigationcontroller xcode4


    【解决方案1】:

    您想要导航控制器在哪里?您必须在 UITabBarController 中为您想要的每个选项卡创建一个。

    您添加一个navigationController 连同其堆栈上的第一个视图控制器。试试这个:

    // create the controllers for UITabBarController
    tableViewController *vc1 = [[[TableViewController alloc] init] autorelease];
    navController *nav1 = [[[UINavigationController alloc] initWithRootViewController:vc1] autorelease];
    
    tableViewController *vc2 = [[[TableViewController alloc] init] autorelease];
    navController *nav2 = [[[UINavigationController alloc] initWithRootViewController:vc2] autorelease];
    
    // put them in an array
    NSArray *controllers = [NSArray arrayWithObjects:nav1, nav2, nil];
    
    // rest of your code
    

    还请注意,您需要释放分配或保留的任何内容。您可以像我一样在初始化它们时添加autorelease,也可以在将它们添加到controllers 数组后显式释放它们。

    然后,您可以在其loadViewviewDidLoad 方法中为每个视图控制器配置navigationItem,具体取决于您的实现方式。

    【讨论】:

    • 感谢我在加载视图中添加了以下内容,但应用程序构建正常,但在第一个导航控制器- (void)loadView { self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(doSomething)]; } - (void)doSomething { }
    • 这是一个不同的问题。如果您使用loadView,您需要通过创建和设置视图属性来初始化视图控制器。因为您的 VC 是 UITableViewControllers,所以您还必须设置 tableView 属性。如果您认为您已正确完成所有操作,请发布您的完整 loadView 代码,我会看看。
    • 我相信我已经设置了表格,因为它在添加导航控制器之前可以正常工作
    • 嘿嘿不知道发生了什么(但又开始了),现在工作就像做梦一样。真的应该注意我得到的可可书,它建议每晚睡 10 小时!栏上还没有按钮,因为它在 loadView 中没有任何工作......
    • 很高兴你能成功。查看有关以编程方式创建 UIViewController 的 Apple 文档,了解您的 loadView 实现的要求。此外,在注释中的代码示例中,您需要将autorelease 添加到您的UIBarButtonItem。正如你所拥有的,它会因为它被保留两次而泄漏。一次分配时,然后由导航项再次分配。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 2016-05-03
    • 1970-01-01
    • 2017-06-26
    相关资源
    最近更新 更多