prepareForSegue是在视图控制器之间传递值的正确方法 [英] Is prepareForSegue right way of passing value between view controllers

查看:113
本文介绍了prepareForSegue是在视图控制器之间传递值的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习Swift,我正在尝试开发着名的笔记应用程序。

I'm trying to learn Swift and I'm trying to develop the famous note application.

有一个绑定到tableview的数组和另一个用于添加的视图笔记。
在第二个视图中,textfieldshouldreturn事件会触发一个segue并返回到tableview。

There is an array bound to a tableview and another view for adding notes. At second view textfieldshouldreturn event triggers a segue and goes back to tableview.

我想知道这是否正确。因为通过这种方式,我正在操纵另一个视图控制器中的变量。我不是MVC大师,但我觉得这是错的。这是我的代码片段:

I wanted to learn if this is the right way. Because by doing this way I'm manipulating a variable in another view controller. I'm not a MVC master but I felt like it is wrong. Here is my code snippet:

func textFieldShouldReturn(textField: UITextField) -> Bool {

    self.performSegueWithIdentifier("backSegue", sender: self)
    return true
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "backSegue"){
        let navController = segue.destinationViewController as UINavigationController;
        let myController = navController.topViewController as NotesTableViewController;
        if(self.ourTextField?.text != nil || self.ourTextField?.text != ""){
            myController.notes.append(self.ourTextField?.text ?? "");
        }

    }
}

谢谢。

推荐答案

你的问题不是关于 prepareForSegue 而是关系查看控制器。你的设计感觉不对的原因在于它。问题是你的笔记编写视图控制器对使用它的视图控制器了解太多,因为它直接操作来自调用视图控制器的变量。为了直接操作变量,它必须知道调用者的类。

Your question is not really about prepareForSegue but the relationship between view controllers. The reason that your design "feels wrong" is that it is. The problem is that your note writing view controller knows too much about the view controller that is using it because it is directly manipulating a variable from the calling view controller. In order to directly manipulate the variable, it must know the class of the caller.

为什么这是一个问题?它使您的笔记编写视图控制器更少可重用。如果您正确编写了笔记编写视图控制器,那么您可以在其他应用程序中重复使用它。为了使其可重复使用,您需要将注释写入视图控制器与调用者分离 - 它必须不知道究竟是谁调用它。

Why is this a problem? It makes your note writing view controller less reusable. If you write the note writing view controller correctly, then you could reuse it in other apps. To make it reusable, you need to decouple the note writing view controller from the caller - it must not know who exactly is calling it.

所以问题变成了,怎么做如果我不知道是谁打电话给我,我会将数据传回给来电者?答案是委派

So the question becomes, how do I pass data back to the caller if I don't know who called me? The answer is delegation.

委托的工作原理如下:


  1. 您创建协议,它描述了该协议的实现者将实现的一种或多种方法。在您的情况下,您可以使用 NoteWriterDelegate 之类的协议来实现方法 takeNote(注意:字符串)

  1. You create a protocol which describes a method or methods that the implementor of that protocol will implement. In your case, you could use a protocol like NoteWriterDelegate that implements the method takeNote(note: String).

protocol NoteWriterDelegate {
    func takeNote(note: String)
}

在文件中与笔记编写视图控制器一起定义。

Define this in the file along with your note writing view controller.

您的笔记编写者将有一个指向委托的可选指针:

Your note writer will have an optional pointer to the delegate:

weak var delegate: NoteWriterDelegate?


  • 您需要将第一个视图控制器声明为 NoteWriterDelegate

    class ViewController: UITableViewController, NoteWriterDelegate
    


  • 然后在第一个视图控制器中实现所需的方法:

  • And then implement the required method in your first view controller:

    func takeNote(note: String) {
        notes.append(note)
    }
    


  • 当你打电话给 prepareForSegue 以准备将移动到笔记写入视图控制器时,你将自己作为委托:

  • When you call prepareForSegue in preparation for moving to the note writing view controller, you pass yourself as the delegate:

    destinationViewController.delegate = self
    


  • 在便笺写入视图控制器中,当您有一个要回传给来电者的留言时,您可以拨打 takeNote 委托:

    delegate?.takeNote(self.ourTextField?.text ?? "")
    


  • 通过这样做,你的note writer只知道它正在与 NoteWriterDelegate 交谈。如果你想在将来重用它,你只需将你的笔记编写器类放到另一个项目中,实现委托,它就可以在你不必触及笔记编写器类中的代码的情况下工作。

    By doing it this way, your note writer only knows that it is talking to a NoteWriterDelegate. If you want to reuse this in the future, you just drop your note writer class into another project, implement the delegate, and it works without you having to touch the code in the note writer class.

    这篇关于prepareForSegue是在视图控制器之间传递值的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆