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
Related
enter image description herei am making a program with delegate protocol in which we add value in text field in second controller and click save and this value is set to the label in the previous viewController
all the this are done but the value can not set to the label
Can someone please explain me what is triggering the messageData() method in the main ViewController?
import UIKit
protocol SenderViewControllerDelegate {
func messageData(data: String)
}
class NewViewController: UIViewController {
//MARK:- Properties
#IBOutlet weak var firstTextField: UITextField!
var delegate: SenderViewControllerDelegate?
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 SaveFirstValue(_ sender: Any) {
if delegate != nil{
if firstTextField.text != nil{
let data = firstTextField.text
delegate?.messageData(data: data!)
dismiss(animated: true, completion: nil)
}
}
}
}
import UIKit
class ViewController: UIViewController,SenderViewControllerDelegate {
//MARK:- Properties
#IBOutlet var firstLabel: 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.
}
//MARK:- Actions
func messageData(data: String) {
firstLabel.text = data
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showSendingVc" {
let viewController : ViewController = segue.destination as! NewViewController
ViewController.delegate = self
}
}
}
Change this line
let viewController = segue.destination as! NewViewController
viewController.delegate = self
`ViewController.delegate = self` should be `viewController.delegate = self`
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showSendingVc" {
let viewController : NewViewController = segue.destination as! NewViewController //destination is type NewViewController not ViewController
// ViewController.delegate = self //Wrong
viewController.delegate = self
}
}
You made mistake in the line ViewController.delegate = self it should be
viewController.delegate = self
means small "v" (object of ViewController)
UPDATED:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showSendingVc" {
let objectOfVC = segue.destination as! NewViewController
objectOfVC.delegate = self
}
}
In ViewController Class set the delegate inside ViewDidLoad method like this:
newViewControllerObj. delegate = self
Without calling viewController Class, the delegate will not set So you have to call ViewController class inside NewViewController or other class which will call first like that:
let ViewControllerObj = ViewController()
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
}
}
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"
}
I am sure I have two view controllers, that have the appropriate files as their classes. (ViewController1.swift and ViewController2.swift) I am trying to pass data from one of the view controllers to the other one with the following code; but I get the error "UIView Controller is not convertible to ViewController2". What should I do? I tried to change as to as! and var to let; neither worked.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!){
if (segue.identifier == "btnSubmitSegue") {
var svc = segue.destinationViewController as ViewController2;
svc.DataPassed = textField.text
}
}
the code for ViewController2
import UIKit
class ViewController2: UIViewController {
var toPass: String = ""
#IBOutlet weak var labeld: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.labeld.text = "passed: \(self.toPass)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
the code for ViewController1
import UIKit
class ViewController1: UIViewController {
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.
}
#IBOutlet weak var textField: UITextField!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "TheSegue" {
let vc = segue.destinationViewController as! ViewController2
vc.toPass = self.textField.text!
}
}
}
The following example works fine for me. Code for the first view controller:
class ViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "TheSegue" {
let vc = segue.destinationViewController as! ViewController2
vc.passedText = self.textField.text!
}
}
}
And in the second view controller:
class ViewController2: UIViewController {
var passedText: String = ""
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.label.text = "passed: \(self.passedText)"
}
}
Don't forget to name the segue in the Storyboard:
Your destination view controller is referring to the wrong controller. You should have this in your code, not as! ViewController1:
let vc = segue.destinationViewController as! ViewController2
Also need to fix your IBOutlet name, the vc.toPass = self.textField.text! should be:
vc.toPass = self.labeld.text!
Make sure the custom class is set for the second view controller:
It may just be that ViewController2 is not declared as a subclass as UIViewController.
At the top of the ViewController2 file you would declare it as follows:
class ViewController2: UIViewController {
\\ ...
}
Have you set the name in the Storyboard ? Under custom class, Class needs to point to the view controllers associated class.
this class is not key value coding-compliant for the key
labelPassedData.' But I do not even have a variable like
labelPassedData
Click you view controller and press right hand arrow button, it shows all connections.
I guess you copy and pasted the view controllers and hence you get this problem.
I use Swift and Xcode 6.4 and I would like to pass a variable from one View Controller to another using a Segue.
When you press the button, the variable duration should give the value to the ViewController2. In the ViewController2 I want to use this variable again.
Main.storyboard button identifier called Test1
How can I do that? Can you help me with my code? Whats wrong?
Thanks
Code ViewController1:
import UIKit
class ViewController1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func ButtonTapped(sender: AnyObject) {
var duration = 1.0
self.performSegueWithIdentifier("Test1", sender: duration)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier = "Test1") {
let secondViewController = segue.destinationViewController as ViewController2
let duration = sender as Double
secondViewController.duration = duration
}
}
}
Code ViewController2:
import UIKit
class ViewController2: UIViewController {
var duration:Double?
var result = duration + 2.0
println(\(result))
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
var duration = Double() // now "duration" is global
#IBAction func ButtonTapped(sender: AnyObject) {
duration = 1.0
self.performSegueWithIdentifier("Test1", sender: duration)
// VVV Any new variable created in this scope will be deallocated
} // HERE
//Then this scope starts...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "Test1") {
let secondViewController = segue.destinationViewController as ViewController2
// delete this... duration = sender as Double
secondViewController.duration = duration
}
}