How to Pass information Back in iOS when reversing

2019-07-08 11:40发布

问题:

I'm new to iOS Development, and Swift. And many of the resources I've found on the Google are written in Objective-C, which I am not familiar also.

I have two view Storyboards, S1 and S2. Each of them have at least 1 View Controller, VC1 and VC2 respectively. I go from S1 to S2 using a Gesture Recognizer, not a Segue. On S2, I select a data taken from a Table View. When I press a value from the list on the table, I would like to send that values back to S1.

What's the best way to do this using Swift? Thanks!

回答1:

You can use NSNotificationCenter for that

Add Oberver in S1

NSNotificationCenter.defaultCenter().addObserver(self, selector: "NotificationMethod:", name:"NotificationName", object: nil)

func NotificationMethod(notification: NSNotification){
    //Take Action on Notification
}

Call it from S2 tableview click and pass userInfo.

NSNotificationCenter.defaultCenter().postNotificationName("NotificationName", object: nil)


回答2:

You can use Custom protocol techniques.

Write in S1

@IBOutlet var textField: UITextField! 
@IBOutlet var datalbl: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.navigationBarHidden = true

}

func myMethod(text:String){
    datalbl.text = text
}

@IBAction func nextViewBtn(sender:UIButton){
    let nextObjvc = self.storyboard?.instantiateViewControllerWithIdentifier("S2") as! S2
    nextObjvc.delegate = self
    self.navigationController?.pushViewController(nextObjvc, animated: true)
}

Write in S2

@IBOutlet var fullNameLabel: UILabel!
var str:String = String()
var myArray:NSArray = NSArray()
var delegate:myProtocol?

override func viewDidLoad() {
    super.viewDidLoad()

    myArray = ["APPLE","BLACKBERRY","CANON","DELEGAT","EMIT","FLAG","GENERAL"]

    self.fullNameLabel.text = str
}

Hope it may help full for you.



回答3:

The preferred way for this would be to use delegation, e.g. tell S2 to report back to S1.

Another answer tried to make this point already, yet didn't make clear what is going on.

The idea is to have S2 hold a reference to S1 and report back. It comes in handy that you are not using segues; they would make it minimally more complex.

Protocol

You can do so by creating a protocol for communication:

protocol ReportBackProtocol {
  func reportValues(someValues : SomeType)
}

S1

Within S1, you implement ReportBackProtocol like so:

class S1 : UIViewController, ReportBackProtocol {

// all your S1 class things


func reportValues(someValues : SomeType) {


   // do with the values whatever you like in the scope of S1
}

And assign your S1 instance as a delegate of your S2 instance where you create it, before you present it:

let s2 = S2()
s2.delegate = self
// present it as you already do

S2

In S2, wherever you have compiled your values (let's call them x) and are ready to send them back:

self.delegate?.reportValues(x)

You also need to add the delegate instance variable to S2 like so:

 weak var delegate : ReportBackProtocol?

This should do the trick; please let me know if you run into problems or do not understand parts of the concept.