我有一个嵌入 RootViewController 中的导航 Controller 。 RootViewController 中存在的元素是分段控件和 UIView。当选择不同的段时,UIView 显示相应的 ViewController。
现在 UIView 中显示的每个 ViewController 都是一个 UITableViewController。
我需要根据段选择显示表值。而且,如果选择了表中的任何行,它必须移动到具有详细 View 的较新的 ViewController。
我能够根据段更改显示 UITableViewControllers。但是,当我单击表格的行时,出现的新 ViewController 是空白的(黑屏),即使我已经在 Storyboard 中创建了特定的 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 为我推送了一个空 View ,尽管我已经指定了要在 Storyboard 设计中使用的 ViewController 并且还使用了标识符。
请帮助我了解我必须采取哪些措施来纠正此错误。
更新 - 上述问题已解决。
我现在面临的问题是无法访问newViewController中的数据。
我将数据传递给 newViewController,如下所示:
创建一个名为 newClass 的类,其中包含我想要传递的所有元素。
在第一个ViewController中创建一个NSMutableArray“newObjects”。
在此方法中创建每个 newClass 对象。
<ul> <li>(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {</li> </ul> <p>static NSString *CellIdentifier = @"Cell";</p> <p>customBigTableCell *cell = (customBigTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];</p> <p>if (cell == nil) { cell = [[customBigTableCell alloc] init]; }</p> <p>cell.name.text = [object objectForKey:@"objName"];</p> <p>newClass* newObj = [[newClass alloc] init];</p> <p>newObj.objName = cell.name.text;</p> <p>[newObjects addObject:newObj];</p>
return cell; }
请原谅我..因为代码块的缩进被搞乱了... 另外 PFObject 是因为我使用 Parse 作为数据库。将创建的对象添加到 newObjects 数组中。
在
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
方法,这是代码(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [ super tableView:tableView didSelectRowAtIndexPath:indexPath];
UIStoryboard*storyboard = [UIStoryboardstoryboardWithName:@"MainStoryboard"bundle:[NSBundle mainBundle]];
newDetailController* newController = [ Storyboard instantiateViewControllerWithIdentifier:@"UpcomingDetail"];
obj = [[newClass alloc] init];
NSIndexPath* 路径 = [self.tableView indexPathForSelectedRow];
obj = [newObjects objectAtIndex:path.row];
NSLog(@"选择的行是 %ld", (long)path.row);//这将返回选择的正确行
NSLog(@"Movie selected is %@", [[newObjects objectAtIndex:path.row] objName]);//但是根据 cellForRowAtIndexPath 方法,这不是正确的。
[newController setNewObj:obj];
[[self navigationController] PushViewController:newController 动画:YES];
当我运行此代码时,该行被正确选择。但是我没有在数据库中得到相应的行。该数组存储有冗余数据。
最佳答案
你需要去掉那条中间线:
newController = [[newDetailController alloc] init];
这只是重新定义了您在第一行中定义的 newController (作为一个没有设置 View 的空 Controller )。
关于ios - 如何制作pushViewController : work in the following case?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14812976/