How to create segue using multiple buttons to a webView - ios

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"
}

Related

iOS swift prepareForSegue cannot pass data between two 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
}
}

#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
}
}

prepareForSegue function not working

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.

Swift: Segue into a UITableView Search

I want to segue into a UITableView Search. I already have the uitableview and search bar implemented and working. Now I want to add a button, that when clicked, opens up the search view with the keyboard out. How can I implement this?
Challenge: How can I put this button on separate view controller?
I have looked up ways to segue into a search bar, but all I have found are ways to segue out of a search.
My Code:
/*Search*/
var searched: Bool = false
#IBOutlet weak var searchBarReal: UISearchBar!
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
getSortedSectionList()
tableView.reloadData()
if searched {
searchBarReal.becomeFirstResponder()
searched = false
}
}
override func viewDidLoad() {
super.viewDidLoad()
let nav = self.navigationController?.navigationBar
nav?.backgroundColor = UIColor.whiteColor()
nav?.tintColor = UIColor.whiteColor()
setUpIcons()
searchBarReal.delegate = self
}
You can tell your searchBar to becomeFirstResponder.
code example:
Initial View Controller
import UIKit
class ViewController: 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.
}
#IBAction func bttnTouched(sender: AnyObject) {
performSegueWithIdentifier("next", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "next" {
let nextVc = segue.destinationViewController as! NextViewController
nextVc.shouldSearchBarRespond = true
}
}
}
Next View Controller
import UIKit
class NextViewController: UIViewController, UISearchBarDelegate {
#IBOutlet weak var searchBar: UISearchBar!
var shouldSearchBarRespond: Bool?
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
if shouldSearchBarRespond == true {
searchBar.becomeFirstResponder()
}
}
}

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!

Resources