【问题标题】:How to create an editing-mode-viewController that gets displayed when UITableViewRowAction is pressed?如何创建按下 UITableViewRowAction 时显示的编辑模式视图控制器?
【发布时间】:2016-09-05 06:44:14
【问题描述】:

在一个 swift to-do 应用中,有一个 UITableViewController 和一个普通的 UIViewController。在名为“AddToDoVC”的 UIViewController 中,有一个用于 todo 名称的文本框和一个标题标签 (viewTitle)“Add ToDo”。现在,添加按钮被按下并保存 toDo 并在“ToDos”UIT​​ableViewController 上显示名称。现在,我插入了一个名为“editAction”的 UITableViewRowAction。当在 tableView 上按下此编辑按钮时,它会转到“AddToDoVC”,我希望该 viewController 进行一些更改,以便成为编辑模式,例如,标题“Add ToDo”更改为“Edit ToDo”或名称 textField 自动成为保存的 toDo 的名称(我正在使用 Realm Swift 进行保存,如果这可能有帮助的话)。现在,我怎么能这样做?这是我到目前为止所写的内容,但它总是给我错误“致命错误:在展开 Optional 值时意外发现 nil。”

在“待办事项”UITableViewController 中:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

    //Edit
    let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Edit") {(editAction, indexPath) -> Void in

        //Shows AddTaskVC when "Edit" is pressed
        let editTaskVC = self.storyboard?.instantiateViewControllerWithIdentifier("addTaskVC") as! AddTaskVC
        self.navigationController?.pushViewController(editTaskVC, animated: true)

        //Changing the viewTitle's text from "Add Task" to "Edit Task" as it is now in an "Edit" mode -> this doesn't do anything other than give me the error "fatal error: unexpectedly found nil while unwrapping an Optional value"
        editTaskVC.viewTitle.text = "Edit Task"

    }

    //Return edit action
    return [editAction]

}

如何实现这种编辑模式?提前谢谢你。

【问题讨论】:

  • 问题解决了吗?

标签: ios swift uitableview uiviewcontroller


【解决方案1】:

而不是将值直接设置为标签。在AddToDoVC 中定义一个字符串str,并将该字符串值分配给viewDidLoad() 中的Label

当前标签的值不会被设置,因为标签没有在视图控制器中绘制并且您正在为其分配值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    相关资源
    最近更新 更多