【问题标题】:push tableViewController in a navigationController with if statement使用 if 语句在 navigationController 中推送 tableViewController
【发布时间】:2012-05-30 17:19:49
【问题描述】:

这是我在 ProfilTableViewController.m 的表视图委托中的 If 语句的摘录:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
UITableViewCell *currentCell =(UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
// Navigation logic may go here. Create and push another view controller.
if (currentCell.textLabel.text==@"phrase") {
    MainViewController *phraseViewController =[[MainViewController alloc] initWithNibName:@"mesPhrase" bundle:nil];
    [self.navigationController pushViewController:phraseViewController animated:YES];
}

在故事板上,我创建了一个从 ProfilTVC 单元到 MainViewController 的推送,并将 TableViewController 更改为“MainViewController”

当我运行应用程序时,单击 ProfilViewController 中的单元格不会执行任何操作:/ 什么鬼?

干杯, 路易斯

【问题讨论】:

  • 为什么不使用 prepareForSegue?
  • 从我阅读(和理解)的内容来看,prepareforsegue 旨在将信息传递给新视图。但是,我使用从单元格到新的 tableviewcontroller 的 segue。我用故事板设置它。我做对了吗?
  • 我实际上是从一个基于标签栏的项目开始的,它运行良好。最后一个没有什么真正有用的,我想我错过了一件大事。谢谢你的帮助!

标签: ios uitableview uinavigationcontroller pushviewcontroller


【解决方案1】:

currentCell.textLabel.text==@"phrase" 比较的是字符串的地址,而不是内容。看isEqualToString:方法。

【讨论】:

  • 好的,我更改了代码,但仍然无法推送视图!
  • if 里面试试NSLog(@"%@ pushing %@", self.navigationController, phraseViewController); 看看它说了什么。
  • 它说:2012-05-31 18:57:30.351 protov03[5300:fb03] (null) push 所以它似乎没有推动新的视图控制器......
  • 所以 (null) 表示无论 self 是什么都没有为它设置导航控制器。
  • 虽然看了很多 tutos 等,但我不知道如何设置导航控制器。nslog 仍然显示“(null) push mainviewcontroller”
【解决方案2】:

现在您所要做的就是使用 prepareFor segue,如下所示:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure we're referring to the correct segue
if ([[segue identifier] isEqualToString:@"yourIdentifier"]) {


   DestinationController *destination = [segue destinationViewController];

    // get the selected index
    NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
    //now you can add informations you want to send to the new controller
    //example:
    destination.selectedItem=[myArray objectAtIndex:selectedIndex];

 }

}

不要忘记更改 segue 标识符:选择两个控制器之间的连线并在右侧的属性检查器中设置它必须等于 youIdentifier

【讨论】:

    猜你喜欢
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多