从表视图控制器推送视图控制器不起作用
我已经设置了一个表视图控制器并连接了一个子视图,因此当我单击行时,会出现新的子视图。我一步一步地遵循了一些教程,但由于某种原因,当我单击行时什么也没有出现(尽管该行正在被选择)。
这是我的主视图控制器:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是导航控制器内的表视图。您可以在 self.navigationController 属性上设置断点
并获取战利品。如果它是 nil,你将无法将任何东西推送到它上面,但它不会给你一个错误,因为在大多数情况下向 nil 发送消息只会返回 nil。
Is the table view inside a navigation controller. You can put a breakpoint on
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.
可能有点小过头了。如果您从模板创建了DetailedViewController,并检查为其创建nib 文件,则nib 文件应与.h 和.m 文件具有相同的名称。因此,请检查您的笔尖类别的名称。它可能应该是:
PS use
self.
here:否则你会泄漏。
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:
P.S. use
self.
here:Otherwise you will leak.