【发布时间】:2013-01-26 14:29:45
【问题描述】:
我在 RootViewController 中嵌入了一个导航控制器。 RootViewController 中存在的元素是分段控件和 UIView。 As the different segments are selected, the UIView displays a corresponding ViewController.
现在在 UIView 中显示的每个 ViewController 都是一个 UITableViewController。
我需要根据段选择显示表格值。而且,如果选择了表格的任何一行,它必须移动到具有详细视图的更新的 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];}
即使我已指定要在 Storyboard 设计中使用的 ViewController 并且还使用标识符,pushViewController 也会为我推送一个空视图。
请帮助我理解我必须做些什么来纠正这个错误。
更新 - 上述问题已解决。
我现在面临的问题是无法访问 newViewController 中的数据。
我将数据传递给newViewController如下:
创建一个名为 newClass 的类,其中包含我想要传递的所有元素。
在 firstViewController 中创建一个 NSMutableArray “newObjects”。
-
在此方法中创建每个 newClass 对象。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
静态 NSString *CellIdentifier = @"Cell";
customBigTableCell *cell = (customBigTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格 == nil){ cell = [[customBigTableCell alloc] init]; }
cell.name.text = [object objectForKey:@"objName"];
newClass* newObj = [[newClass alloc] init];
newObj.objName = cell.name.text;
[newObjects addObject:newObj];
返回单元格; } 请原谅我..因为代码块的缩进搞砸了...... PFObject 也是因为我使用 Parse 作为数据库。
将创建的对象添加到 newObjects 数组中。
-
在
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法中,这是代码-
(void)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];
NSLog(@"选择的行是 %ld", (long)path.row); //这会返回选择的正确行
NSLog(@"Movie selected is %@", [[newObjects objectAtIndex:path.row] objName]);//但是根据 cellForRowAtIndexPath 方法,这不是正确的。
[newController setNewObj:obj];
[[self navigationController] pushViewController:newController Animation:YES]; }
-
当我运行此代码时,该行被正确选择。但是我没有在数据库中得到相应的行。数组存储有冗余数据。
【问题讨论】:
标签: ios uinavigationcontroller pushviewcontroller