【问题标题】:Move to another viewcontroller from button action of a custom tableviewcell从自定义 tableviewcell 的按钮操作移动到另一个视图控制器
【发布时间】:2018-01-25 09:55:09
【问题描述】:

我在表格视图的自定义表格视图单元格中有一个按钮。 我尝试向另一个视图控制器展示、推送和实例化,但因显示错误而失败:

线程 1:致命错误:在展开 Optional 时意外发现 nil

这是我的代码: console image if required

class ProfileHeaderView: UITableViewCell{

    @IBAction func editProfile(_ sender: Any) {

        let editProfilePage = EditProfileViewController()
        UIApplication.shared.keyWindow?.rootViewController?.present(editProfilePage, animated: true, completion: nil)

    } 

}

我想移动的页面:

class EditProfileViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    override func viewDidLoad() {
        super.viewDidLoad()

        self.hideKeyboardWhenTappedAround()

        mobileNumber.keyboardType = UIKeyboardType.numberPad **//Error**
    }
}

我也尝试通过rootViewController 进行演示。请帮忙。

Screenshot if required

Updated console after the story board identifier change

【问题讨论】:

  • 可能重复:link
  • 您是否尝试过显示该视图控制器?测试问题是与 tableView 相关还是仅与 EditProfileViewController 相关
  • @Milan Nosal。我希望这是 EditProfileViewcontroller 的问题。但是为什么它说那些小键盘在从滑动菜单控制器导航时工作正常时为零。
  • @NiranjanB 我相信我知道出了什么问题,请检查我的答案

标签: ios swift uitableview navigation


【解决方案1】:

解释

崩溃是由mobileNumber.keyboardType = UIKeyboardType.numberPad 行引起的,不是因为UIKeyboardType.numberPad,而是因为左侧:mobileNumber.keyboardType。手机号码应该是UITextField! 类型的@IBOutlet(隐式强制解包UITextField),当mobileNumber 为nil 时会导致崩溃。这就是你的情况。

它以前可以工作,因为在您使用 segue 导航到 EditProfileViewController 之前,故事板在调用 viewDidLoad 之前正确初始化了 mobileNumber 字段。但是,您正在以下行以编程方式创建EditProfileViewController 的实例:let editProfilePage = EditProfileViewController(),这完全绕过了情节提要。因此,应该由故事板连接的字段和操作不会连接,从而导致崩溃。

解决方案

使用故事板创建EditProfileViewController 的实例,而不是调用初始化程序。这意味着,替换这一行:

let editProfilePage = EditProfileViewController()

这样的:

let editProfilePage = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "EditProfileViewController") as! EditProfileViewController

在这一行中,我假设带有EditProfileViewController 的情节提要被命名为Main(当然,如果EditProfileViewController 是在其他情节提要中定义的,请使用它而不是那里的Main 情节提要),并确保您在情节提要中使用情节提要 ID 并将其设置为 EditProfileViewController(或使用您想要的任何自定义 ID):

【讨论】:

  • 我也试过这个。但显示错误:libc++abi.dylib:以 NSException 类型的未捕获异常终止。
  • @NiranjanB 但错误发生了变化,所以我相信我们正在做一些事情......你能分享整个新消息 + 堆栈跟踪吗?
  • 这是这个应用程序挂在那里后的完整消息。
  • @NiranjanB 那一刻的xcode截图怎么样?
  • 无法将图像添加到 cmets 部分,我可以知道怎么做吗?
【解决方案2】:

我用以下代码为自己解决了这个问题 您可以使用协议将 tableviewcell 内的点击委托给 UIViewController 就我而言,我确实使用了 segues,但您可以直接在 cellCallback 函数中实例化您的 EditProfileViewController

委托代码

protocol CellDelegator {
    func cellCallback(myData dataobject: Item)
}

表格视图单元格内的代码

@IBAction func imageClicked(_ sender: UIButton) {
    self.delegate?.cellCallback(myData: (self.cartItem?.item)!)

}

ViewController 内的代码

extension MyViewController :  CellDelegator{
    func cellCallback(myData dataobject: Item) {
        performSegue(withIdentifier: "xyz", sender: dataobject)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2013-11-03
    相关资源
    最近更新 更多