iOS swift prepareForSegue cannot pass data between two viewcontroller - ios

This is my code of two viewcontroller, need pass "numberToDisplay" from class bookTwo to class bookThree, but it not work, always show 0 in bookThree.
I am using swift3 on ios 10, xocod 8.
bookTwo:
import UIKit
import Foundation
class bookTwo: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ms1"{
let destinationVC = segue.destination as? bookThree;
destinationVC?.numberToDisplay = 7
}
}
}
bookThree
import UIKit
class bookThree: UIViewController {
var numberToDisplay = 0
#IBOutlet weak var showType: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
showType.text = "Tapped \(numberToDisplay) times."
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

In Swift 3, the signature for method "prepareForSegue" has changed. So replace the code for "prepareForSegue" with this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ms1"{
let destinationVC = segue.destination as? bookThree
destinationVC?.numberToDisplay = 7
}
}

Related

viewDidLoad() load twice when passing data with Navigation Controller - SWIFT

I'm new to swift development. I'm trying to pass data from one UiViewController to Another UIViewController with NavigationController. I was able to pass the data, but for some reason, it loads the viewDidLoad() twice on the second UIViewController. Below is my code.
ViewController
class ViewController: UIViewController {
var option = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// Dine In Btn
#IBAction func dineinBtn(_ sender: Any) {
self.option = "Dine In"
performSegue(withIdentifier: "dinein", sender: self)
}
// Take Out Btn
#IBAction func takeoutBtn(_ sender: Any) {
self.option = "Take Out"
performSegue(withIdentifier: "takeout", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! QualityViewController
vc.option = self.option
}
#IBAction func unwindToVC1(segue:UIStoryboardSegue) { }
}
QualityViewController
class QualityViewController: UIViewController {
var option = ""
override func viewDidLoad() {
super.viewDidLoad()
option += option
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
The reason may be that you hook buttons dineinBtn and takeoutBtn as the segue source , so the IB segue is triggered plus this
performSegue(withIdentifier: "takeout", sender: self)
Another reason may be that you copied the buttons in IB so each one is hooked to 2 IBActions in code

#IBAction to performSegue

I have the following:
class Settings: UIViewController {
#IBAction func CitySend(sender: AnyObject) {
self.performSegue(withIdentifier: "senddata", sender: self)
}
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "senddata" {
let Mainview = segue.destination as! ViewController
Mainview.label = textField.text!
}
}
}
My mainview looks like this:
class ViewController: UIViewController {
#IBOutlet var city: UILabel!
var label: String = ""
override func viewDidLoad() {
super.viewDidLoad()
city.text = label
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
im not sure, if this is how i should even do this, but to me the logic is that i only want to allow this segue to pass a string to my label in my mainview if the button is pressed (Like a save button)
However as to what i have made now, nothing happens, and it gives me the 1: signal SIGABRT error, whenever i press the button.
Use the following code:
ViewController.swift
import UIKit
class ViewController: UIViewController, SecondViewControllerProtocol{
#IBOutlet weak var dataLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func navigateToSecondViewControllerOnButtonClick(_ sender: AnyObject) {
let secondVC: ViewController2 = storyboard?.instantiateViewController(withIdentifier: "viewcont2") as! ViewController2
secondVC.delegate = self
self.navigationController?.pushViewController(secondVC, animated: true)
}
func saveData(data: String){
dataLabel.text = data
}
}
ViewController2.swift
import UIKit
protocol SecondViewControllerProtocol {
func saveData(data: String) // this function the first controllers
}
class ViewController2: UIViewController {
var delegate: SecondViewControllerProtocol?
#IBOutlet weak var dataTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func saveButtonClicked(_ sender: AnyObject) {
delegate?.saveData(data: dataTextField.text!)
}
}
Storyboard screenshot:
Please check my GitHub link below to test sample:
https://github.com/k-sathireddy/PassingDataThroughSegue
#IBAction func CitySend(sender: AnyObject) {
self.performSegue(withIdentifier: "senddata", sender: textField.text)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch (segue.identifier, segue.destination, sender) {
case (.Some("senddata"), let controller as ViewController, let text as String):
controller.label = text
default:
break
}
}

How to create segue using multiple buttons to a webView

I have two viewcontroller. I am trying to load multiple url in one webview using buttons. My first viewcontroller contains buttons and second viewcontroller contains a uiwebview. I have connected two buttons to the webview by control+click+dragging.Here is my code. It doesn't work. What's the problem?
first vc:
import UIKit
class ViewController: UIViewController {
#IBOutlet var link1: UIButton!
#IBOutlet var link2: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func link1(sender: AnyObject) {
self.performSegueWithIdentifier("link1", sender: sender)
}
#IBAction func link2(sender: AnyObject) {
self.performSegueWithIdentifier("link2", sender: sender)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
let websiteController = segue.destinationViewController as webViewController
if segue.identifier == "link1" {
websiteController.urlWebsite = "www.google.com"
} else if segue.identifier == "link2" {
websiteController.urlWebsite = "http://www.stackoverflow.com"
}
}
}
second vc:
import UIKit
class webViewController: UIViewController {
#IBOutlet var webView: UIWebView!
var urlWebsite: String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let myURLString = urlWebsite
let myURL = NSURL(string: myURLString!)
let myURLRequest = NSURLRequest(URL: myURL!)
webView.loadRequest(myURLRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I think, your issue is in this line of code :
if segue.identifier == "link1" {
websiteController.urlWebsite = "www.google.com"
}
you need to add prefix http:// in url . so the your code is like below
if segue.identifier == "link1" {
websiteController.urlWebsite = "http://www.google.com"
}

how can make a button goes to the next activity and passing its titleLabel in a string as well?

can anyone please guide me on how to make a button when its clicked goes to the next activity and pass its data to the next button
this is my code
class SecondViewController: UIViewController {
#IBOutlet weak var electronics: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var pass:searchResultView = segue.destinationViewController as! searchResultView
if(electronics.selected){
pass.received = electronics.titleLabel!.text!
}
}
when i run the app and click the button, it crashes!
here is the code in the searchResultView
class searchResultView: UIViewController {
var received:String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I tried to do re-write the code this way
#IBOutlet weak var electronics: UIButton!
#IBAction func electronics(sender: AnyObject) {
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var pass:searchResultView = segue.destinationViewController as! searchResultView
pass.received = electronics.titleLabel!.text!
}
}
by implementing the code that passes the data inside the button itself
the app still crashes!

Pass one variable from one viewcontroller to another viewcontroller

I new to swift language and know this question is duplicated.I`ve found several similar question and answer, but i could not able to figure out the problem.
I want to pass the value of detectionString variable to ResultViewController from ScanViewController.
ScanViewcontroller as below:
import UIkit
class ScanViewController: UIViewController {
var detectionString : String!
override func viewDidLoad() {
super.viewDidLoad()
detectionString = “SomeDetectedString”
}
override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject!) {
if (segue.identifier == "MySegue") {
var svc = segue.destinationViewController as ResultViewController;
svc.detectedString = detectionString
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
The ResultViewController as below:
import UIkit
class ResultViewController: UIViewController {
var detectedString: String!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor=UIColor.whiteColor()
println(detectedString)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
The println(detectedString) gives me no result. question is that how can get the variable from ScanViewController?
This may sound bizarre but there is nothing wrong with your code BUT it does not work as is. I used your code identically and it ignored the segue. Then I embedded ScanViewController in a navigation controller in the storyboard. I also put a call to self.performSegueWithIdentifier("MySegue", sender: self) in the ScanViewController viewDidLoad to initiate the segue. Then everything works like a charm. Your prepareForSegue is fine. Yuvrajsinh's suggestion is fine but not necessary (I tried it after changing DetailVC to ResultViewController). Without the navigation controller nothing works. segue.identifier is a string and it will work in a straight Swift string comparison.
Here is the code for ScanViewController:
import UIkit
class ScanViewController: UIViewController {
var detectionString : String!
override func viewDidLoad() {
super.viewDidLoad()
detectionString = "SomeDetectedString"
println(detectionString)
self.performSegueWithIdentifier("MySegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject!) {
if (segue.identifier == "MySegue" || segue.identifier == "SegueFromButton") {
println("prepareForSegue")
var svc = segue.destinationViewController as ResultViewController;
svc.detectedString = detectionString
println("svc.detectedString: \(svc.detectedString)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
and for ResultViewController:
import UIkit
class ResultViewController: UIViewController {
var detectedString: String!
override func viewDidLoad() {
println("Result Load View")
super.viewDidLoad()
self.view.backgroundColor=UIColor.whiteColor()
println("detectedString in Result: \(detectedString)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
your segue.identifier == "MySegue" is some how not comparing in the way it should.
replace your code with following function and you are done.
override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject!) {
let segueName: String = segue.identifier!;
if (segueName == "MySegue") {
var svc = segue.destinationViewController as DetailVC;
svc.detectedString = detectionString
}
}
There are three options for you:
use prepareForSeguemethod to get the target ViewController and set up the property
set up delegate relationship between the two ViewControllers to communicate
set up Observer with NSNotificationCenter

Resources