附注如何将导航栏上的后退按钮从“
我发现这个老问题在 Objective C 中有答案,How to change text on a back button
您必须在您要从中进行切换的视图控制器中进行设置。
假设我们有一个简单的 iPhone 应用程序,它有一个 UITableViewController 和一堆列出数字的单元格,我们称之为“NumberListTVC”。我们将此 NumberListTVC 嵌入到 NavigationController 中。我们将其中一个单元格上的选择序列设置为另一个 ViewController,我们将其称为“NumberDisplayerVC”。这个 NumberDisplayerVC 有一个标签,我们将把它设置为在 NumberList 上单击的标签的内容。因此,当您在 NumberListTVC 中选择一个单元格时,它将推送到 NavigationController 中的“NumberDisplayer”。
所以对于这个应用程序,您可以将自定义后退按钮的代码放在 NumberListTVC 中,这样当您查看 NumberDisplayerVC 时,它会显示我们想要的后退按钮,而不是默认按钮(因为后退按钮将其带回 NumberListTVC)。
在 Objective-C 中,根据链接的答案,将“
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
基本上,我们创建了一个新的 UIBarButtonItem ,它具有我们想要的任何标题。在这种情况下,我只是给它的标题“”,它只是一个空字符串。
在 Swift 中同样的命令是:
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style:.Plain, target: nil, action: nil)
我把它放在 NumberListTVC 的 viewDidLoad 方法中,以确保它在 NumberListTVC 加载时运行。
附言如何让 tableViewCell 在按下后自动释放,使其不会保持突出显示?
这在 Objective-C 中的答案 Selected UItableViewCell staying blue when selected 中有介绍
您只需告诉 UITableViewCell 在其 didSelectRowAtIndexPath 中取消选择它。
在 Objective-C 中,您可以覆盖它并执行以下操作:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//在这里做任何你想做的事情...
[tableView deselectRowAtIndexPath:indexPath 动画:YES];
}
Swift 代码类似:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
//Do whatever else you want to do here...
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
P.S.S.S 如何根据用户单击的单元格自定义 DetailView?
为简单起见,我将使用我的第一个示例,它只是将我们的 navigationController 与 NumberListTVC 和 NumberDisplayerVC 一起使用,因此不在 UISplitView 上,但提出的问题会影响这两种情况。
基本上,你必须从被点击的单元格中获取你想要的东西,然后将它设置为你想要在下一个 ViewController 中使用的任何东西。因此,在您的 prepareForSegue 中,您将执行以下操作:
if segue.identifier == "amazingSegue"
{
if let cellText = (sender as? UITableViewCell)?.textLabel.text
{
if let someVC = segue.destinationViewController as? NumberDisplayerVC
{
someVC.textToDisplay = cellText
}
}
}
这基本上是安东尼奥在his answer 中所说的。我加了一点。所以在我的例子中,NumberListTVC 是一堆动态创建的 UITableViewCells。我想从每个标签中取出标签,然后在 NumberDisplayerVC 中使用它。所以我们:
- 检查它是否是我们想要实际使用的segue,在这种情况下,NumberListTVC 和 NumberDisplayerVC 之间的 segue 被命名为“amazing segue”。
- 接下来,使用可选绑定将发送方单元格的文本分配给常量
cellText。我们使用可选的类型转换运算符来检查发送者是否是 UITableViewCell。然后,我们使用可选链查看此 UITableViewCell 内部并从其textLabel 中提取text。
- 再次使用可选绑定将我们的
destinationViewController 分配给常量someVC。我们可以选择将其类型转换为 NumberDisplayerVC,这就是它应该的样子。如果它确实是 1,它将正确绑定到 someVC 变量。
- 然后只需将
textToDisplay 属性设置为我们之前可选绑定的cellText 常量。
我不确定这是否是最好的方法,我对 if let 语句的塔感觉不太好,但它最初可以完成工作。