如何使pushViewController:在以下情况下工作?
我有一个嵌入在RootViewController中的导航控制器。存在于RootViewController中的元素是一个分段控件和一个UIView。当选择不同的段时,UIView将显示相应的ViewController。如何使pushViewController:在以下情况下工作?
现在,显示在UIView中的每个ViewController都是一个UITableViewController。
我需要根据段选择显示表值。此外,如果选择了表格的任何一行,则必须使用详细视图移动到更新的ViewController。
我能够根据细分的变化显示UITableViewControllers。然而,当我点击表格的行时,即使我已经在故事板中创建了特定的ViewController并设置了一个标识符,出现的较新的ViewController也是空白的(黑屏)。
代码在ViewController.m分段的控制变化
- (void)segmentedControlChangedValue:(UISegmentedControl*)segmentedControl {
int selIndex = segmentedControl.selectedIndex;
if (selIndex == 0)
{
aTable = [[aTableViewController alloc] init];
aTable.view.frame = self.myView.bounds;
[self.myView addSubview:aTable.view];
[self addChildViewController:aTable];
}
if (selIndex == 1)
{
bTable = [[bTableViewController alloc] init];
bTable.view.frame = self.myView.bounds;
[self.myView addSubview:bTable.view];
[self addChildViewController:bTable];
}
if (selIndex == 2)
{
//NSLog (@"Well you selected a 3rd option");
}}
代码为didSelectRowAtIndexPath方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
newDetailController* newController = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
newController = [[newDetailController alloc] init];
[[self navigationController] pushViewController:newController animated:YES];}
的pushViewController推给我一个空的观点,即使我已指定在Storyboard设计中使用ViewController并使用标识符。
请帮我理解我必须采取什么措施来纠正这个错误。
UPDATE - 上述问题已解决。
我现在面临的问题是无法访问newViewController中的数据。
我的数据传递给newViewController如下:
创建一个名为的NewClass类,其中包含所有我想通过跨越的元素。
在firstViewController中创建一个NSMutableArray“newObjects”。
-
在此方法中创建每个newClass对象。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"Cell";
customBigTableCell *cell = (customBigTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) { cell = [[customBigTableCell alloc] init]; }
cell.name.text = [object objectForKey:@"objName"];
newClass* newObj = [[newClass alloc] init];
newObj.objName = cell.name.text;
[newObjects addObject:newObj];
return cell; }
请原谅我..因为与代码块的缩进是搞砸了...... 另外PFObject是COS我使用数据库的解析。 将创建的对象添加到newObjects数组。
-
在
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
方法,这是代码-
(无效)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath { [超级的tableView:的tableView didSelectRowAtIndexPath方法:indexPath];
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@“MainStoryboard”bundle:[NSBundle mainBundle]];
newDetailController * newController = [storyboard instantiateViewControllerWithIdentifier:@“UpcomingDetail”];
obj = [[newClass alloc] init];
NSIndexPath * path = [self.tableView indexPathForSelectedRow];
obj = [newObjects objectAtIndex:path.row]; (@“Row selected is%ld”,(long)path.row); //这将返回选中的正确行
NSLog(@“Movie selected is%@”,[[newObjects objectAtIndex:path.row] objName]); //然而,根据cellForRowAtIndexPath方法,这不是正确的。
[newController setNewObj:obj];
[[self navigationController] pushViewController:newController animated:YES]; }
-
当运行该代码,该行被正确地选择。但是我没有得到数据库中的相应行。数组与冗余数据一起存储。
你需要摆脱中线:
newController = [[newDetailController alloc] init];
这只是重新定义newController(如无视图建立一个空的控制器),您在第一行定义。
谢谢@rdelmar。但是,当我删除该语句,我得到以下错误... 应用程序试图推动一个无视图控制器的目标 –
AJoseph
2013-02-12 08:10:24
此外,我检查了类似的问题,他们问我不要重写 - (void)loadView方法。但是,我没有在我的程序中重写该方法。 – AJoseph 2013-02-12 08:14:56
@AJoseph,如果你得到这个错误,那么出于某种原因,上面的行不返回视图控制器。你应该记录newController和self.storyboard来查看哪一个是零。此外,你可能不应该重载loadView - 你为什么这样做? – rdelmar 2013-02-12 15:32:49