【问题标题】:TableView Does NOT Switch to Detail View When Connected to Tab Bar Controller连接到选项卡栏控制器时,TableView 不会切换到详细视图
【发布时间】:2010-08-12 05:09:28
【问题描述】:

我创建了一个Tab Bar Application,去掉了默认的FirstView和SecondView,添加了以下两个ViewController:

  1. JustAnotherView(它只是一个 UIViewController,里面有一个 UIView)
  2. MyListOfItems(这是一个 UIViewController,里面有一个 TableView)

我有一个名为 ListOfItemsDetailViewController 的第三个 ViewController,里面有一个 UIView,当用户触摸其中一个 TableView 单元格时应该调用它。

应用程序编译没有问题。我可以同时触摸 Tab Bar 按钮并显示正确的视图。

问题: 当我单击 TableView 单元格时,会执行切换到详细视图的代码(没有崩溃或错误),但视图永远不会显示。

这是应用程序和带有 NSLog 的调试器的屏幕截图,显示它通过 pushViewController 而没有崩溃:

http://clip2net.com/clip/m52549/thumb640/1281588813-9c0ba-161kb.jpg

这里是代码。

MyListOfItemsViewController.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "ListOfItemsDetailViewController.h";

@interface MyListOfItemsViewController : UIViewController {
    ListOfItemsDetailViewController *listOfItemsDetailViewController;
}

@property (nonatomic, retain) ListOfItemsDetailViewController *listOfItemsDetailViewController;


@end

这是 MyListOfItemsViewController.m 文件:

#import "MyListOfItemsViewController.h"
@implementation MyListOfItemsViewController
@synthesize listOfItemsDetailViewController;

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.title = @"My List Of Items";
    NSLog(@"My List Of Items Did Load");
}

#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 5;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    NSString *cellMessage;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cellMessage = [NSString stringWithFormat:@"indexPath: %d",indexPath.row];       
    cell.textLabel.text = cellMessage;
    return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    ListOfItemsDetailViewController *vcListOfItemsDetail = [[ListOfItemsDetailViewController alloc] 
                                    initWithNibName:@"ListOfItemsDetail" bundle:[NSBundle mainBundle]];
    self.listOfItemsDetailViewController = vcListOfItemsDetail;
    [vcListOfItemsDetail release];
    [self.navigationController pushViewController:self.listOfItemsDetailViewController animated:YES];

    NSLog(@"Did Execute didSelectRowAtIndexPath WITHOUT Crashing or Error.");

}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
    NSLog(@"My List Of Items View Did Unload");
}


- (void)dealloc {
    [super dealloc];
}



@end

请帮忙。将不胜感激!!!

我一直在疯狂地尝试在 iOS 平台上实现基本导航。

另外,请原谅外行的问题,我知道你们中的大多数人已经知道如何做到这一点。

提前致谢。 乔森

【问题讨论】:

    标签: iphone uiviewcontroller pushviewcontroller


    【解决方案1】:

    不要将第二个选项卡的 viewcontroller 设置为 tableviewcontroller,而是将其设置为 UINavigationController 并将 navcontroller 的 RootViewController 设置为“MyListOfItems”。完成此操作后,您将可以访问 viewcontroller 中的 navigationController 属性,并且可以使用 push 和 pop。

    或者,如果您不想这样做,剩下的唯一方法是以编程方式在视图之间设置动画(即删除一个视图并添加另一个视图或在当前视图之上添加另一个视图)

    【讨论】:

    • 非常感谢您的回复。这正是我想要的。非常清楚。我对第一个做出了正确的回应,因为他们似乎都在指向同一个方向(因为,我错过了所有重要的 UINavigation 控制器 - 呃,当你回想起来时)。
    • 这确实是帮助我了解发生了什么的链接:stackoverflow.com/questions/284321/…
    【解决方案2】:

    tableView:didSelectRowAtIndexPath: 中,您通过self.navigationController 引用了一个navigationController,这可能不存在。您必须在 tabView 中使用 UINavigationController 作为一个视图控制器,并且该 navigationController 的 rootViewController 应该是 MyListOfItemsViewController。

    请注意,您需要一个 UINavigationController 来推动您的详细视图,而 self.navigationController 只是一种检索方法,它之前已创建并插入到层次结构中。

    【讨论】:

    • 非常感谢。这正是我一直在寻找的。当我得到这个时,iOS 开发中的很多事情变得更加清晰。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多