我是iOS开发的新手,对变量有疑问。我正在尝试使用标签栏和导航控制器编写应用程序,标签栏是rootViewController。我已经将我的应用程序设置为包含一个包含我的表视图行为的.plist,一个UITableViewController和一个详细视图控制器。我在我的初始表视图控制器的实现文件中继续出现一个错误,因为我在我的App委托中定义了导航控制器。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the dictionary of the selected data source.
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
//Get the children of the present item.
NSArray *Children = [dictionary objectForKey:@"Children"];
if([Children count] == 0) {
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
}
else {
//Prepare to tableview.
IndustryTableViewController *industryTableViewController = [[IndustryTableViewController alloc] initWithNibName:@"IndustryTableViewController" bundle:[NSBundle mainBundle]];
//Increment the Current View
industryTableViewController.CurrentLevel += 1;
//Set the title;
industryTableViewController.CurrentTitle = [dictionary objectForKey:@"Title"];
//Push the new table view on the stack
[self.indNavControl pushViewController:industryTableViewController animated:YES];
industryTableViewController.tableDataSource = Children;
[industryTableViewController release];
}
}
当我在堆栈上推送新表视图时出现错误。我正在导入我的应用程序委托的头文件,但它仍然无法工作,给我错误,“在TableViewController.h中未定义变量”。有没有办法让我召唤这个变量,还是有更方便的方法让我解决这个问题?
在此先感谢,我真的可以使用你给我的任何帮助。
答案 0 :(得分:0)
您可以获得指向appDelegate的指针,然后获取您的变量:
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
另一种方法是使用 依赖注入 ,并在创建时将引用注入控制器。所以基本上在控制器中定义一个ivar来保存一个引用然后执行此操作:
[myController new];
[myController setController:otherController];
答案 1 :(得分:0)
您指的是带有self.navigationController
的navigationController。
这意味着 navigationController 是TableViewController类中定义的变量。
您应该在类中定义此变量,并修改tableviewcontroller的init方法以接收该变量。
答案 2 :(得分:0)
它说的哪个变量没有定义?你能分享细节吗?看起来如下: self.indNavControl
这个indNavControl定义在哪里?请正确定义,它应该可以正常工作。