从表视图控制器推送视图控制器不起作用

发布于 2024-11-24 07:43:17 字数 2577 浏览 2 评论 0原文

我已经设置了一个表视图控制器并连接了一个子视图,因此当我单击行时,会出现新的子视图。我一步一步地遵循了一些教程,但由于某种原因,当我单击行时什么也没有出现(尽管该行正在被选择)。

这是我的主视图控制器:

@interface TableViewsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

IBOutlet UITableView *tblSimpleTable;
IBOutlet UINavigationBar *navBar;
NSArray *arryData;
NSMutableArray *listOfItems;
}

@property (nonatomic, retain) IBOutlet UITableView *tblSimpleTable;
@property (nonatomic, retain) IBOutlet UINavigationBar *navBar;
@property (nonatomic, retain) NSArray *arryData;
@property (nonatomic, retain) NSMutableArray *listOfItems;

(相关部分)

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

static NSString *CellIdentifier = @"Cell";

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

NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Computers"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;

}

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

NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Computers"];
NSString *selectedCountry = [array objectAtIndex:indexPath.row];

//Initialize the detail view controller and display it.
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
dvController.selectedComputer = selectedCountry;
[self.navigationController pushViewController:dvController animated:YES];
dvController = nil;

}

和子视图控制器:

.h.m

@interface DetailViewController : UIViewController {

IBOutlet UILabel *lblText;
NSString *selectedComputer;


}

@property (nonatomic, retain) IBOutlet UILabel *lblText;
@property (nonatomic, retain) NSString *selectedComputer;

@end

.m

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

//Display the selected country.
lblText.text = selectedComputer;

//Set the title of the navigation bar
self.navigationItem.title = @"Selected Computer";

}

我很确定IB中的所有内容都连接良好。

感谢您的帮助!

I have setup a table view controller and connected a sub view so when I click on the rows the new subview appears. I followed a few tutorials step by step, but from some reason, nothing comes up when I click on the rows (the row is being selected though).

Here is my main view controller:

@interface TableViewsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

IBOutlet UITableView *tblSimpleTable;
IBOutlet UINavigationBar *navBar;
NSArray *arryData;
NSMutableArray *listOfItems;
}

@property (nonatomic, retain) IBOutlet UITableView *tblSimpleTable;
@property (nonatomic, retain) IBOutlet UINavigationBar *navBar;
@property (nonatomic, retain) NSArray *arryData;
@property (nonatomic, retain) NSMutableArray *listOfItems;

.m (relevant portions)

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

static NSString *CellIdentifier = @"Cell";

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

NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Computers"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;

}

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

NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Computers"];
NSString *selectedCountry = [array objectAtIndex:indexPath.row];

//Initialize the detail view controller and display it.
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
dvController.selectedComputer = selectedCountry;
[self.navigationController pushViewController:dvController animated:YES];
dvController = nil;

}

And the subview controller:

.h

@interface DetailViewController : UIViewController {

IBOutlet UILabel *lblText;
NSString *selectedComputer;


}

@property (nonatomic, retain) IBOutlet UILabel *lblText;
@property (nonatomic, retain) NSString *selectedComputer;

@end

.m

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

//Display the selected country.
lblText.text = selectedComputer;

//Set the title of the navigation bar
self.navigationItem.title = @"Selected Computer";

}

I am pretty sure everything is well connected in IB.

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与他有关 2024-12-01 07:43:17

是导航控制器内的表视图。您可以在 self.navigationController 属性上设置断点

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

并获取战利品。如果它是 nil,你将无法将任何东西推送到它上面,但它不会给你一个错误,因为在大多数情况下向 nil 发送消息只会返回 nil。

Is the table view inside a navigation controller. You can put a breakpoint on

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

and take a loot at the self.navigationController property. if it is nil, you will not be able to push anything onto it but it will not give you an error as sending a message to nil will merely return nil in most cases.

成熟的代价 2024-12-01 07:43:17

可能有点小过头了。如果您从模板创建了DetailedViewController,并检查为其创建nib 文件,则nib 文件应与.h 和.m 文件具有相同的名称。因此,请检查您的笔尖类别的名称。它可能应该是:

DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

PS use self. here:

self.dvController = nil;

否则你会泄漏。

Might be a small over look. If you created your DetailedViewController from a template and you check to create a nib file for it, the nib file should have the same name as the .h and .m files. So check for the name of your nib class. It might should be:

DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

P.S. use self. here:

self.dvController = nil;

Otherwise you will leak.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文