-1

I have two view controllers involved in this issue. The first is "eatMap" and the second is "restaurants." eatMap contains multiple buttons that show pictures and names of restaurants as well as a map with pins of each location. I want the restaurants View Controller to display the the menus of each restaurant in a UIWebView called "eatView". I have an array holding the urls to each website.

How do I instruct the UIWebView to load a different url depending on which button is clicked?

I suspect the problem is in my prepareForSegue function or with my var currentSite or var url, but I'm not sure. I know the urls aren't being passed correctly into theUIWebView.

I'm stuck!

Sorry for all the backslashes. I've been trying to keep track of what all I've tried that has gotten me anywhere.

here is View Controller 1 aka eatMap:

import UIKit
import MapKit


class eatMap: UIViewController {


var siteLink = ["http://amici-cafe.com/athens.html", "http://www.athensbagel.com/", "https://www.facebook.com/dawggonegoodbbq/", "http://www.fuzzystacoshop.com/", "http://places.singleplatform.com/globe/menu?ref=google", "https://www.facebook.com/Kellys-Authentic-Jamaican-Food-333515583330794/", "http://theplaceathens.com/", "http://www.pauleyscrepebar.com/", "http://southkitchenbar.com/", "http://tazikiscafe.com/", "http://www.tedsmostbest.com/", "https://yourpie.com/", "http://trappezepub.com/", "http://www.vivaargentinecuisine.com/", "http://five-bar.com/bar-locations/athens-georgia/"]
var currentSite = 0
var url = String()

@IBAction func button01(_ sender: Any) {
   currentSite=0
    //var url = NSURL(string: currentSite = 0)
    //var url = NSURL(string: siteLink[0])
    //loadPage()
}

@IBAction func button02(_ sender: Any) {
    currentSite+=1
    //var url = NSURL(string: currentSite = 1)
    //var url = NSURL(string: siteLink[1])
    //loadPage()
}

@IBAction func button03(_ sender: Any) {
    currentSite+=2
    //var url = NSURL(string: currentSite = 2)
   // var url = NSURL(string: siteLink[2])
    //loadPage()
}

//func loadPage(){
   //let url = NSURL(string: self.url)
    //(string: siteLink[currentSite])
    //let request = NSURLRequest(url:url as! URL)
    //self.eatView.loadRequest(request as URLRequest)
   //}


@IBOutlet weak var mainScrollView: UIScrollView!

var myTitles = ["Amici", "Athens Bagel Co.", "Dawg Gone Good BBQ", "Fuzzy's Taco Shop", "The Globe", "Kelly's Jamaican Foods", "The Place", "Pauley's", "South Kitchen and Bar", "Taziki's", "Ted's Most Best", "Your Pie", "Trappeze Pub", "Viva Argentine Cuisine", "FIVE Athens"]
var mySubtitles = ["Casual Italian dining", "Bagels",  "No-nonsense outpost for BBQ & sides", "Sleek Mexican counter-service chain", "Laid-back hangout with pub grub & beer", "Unfussy stop for jerk chicken & curries", "Chill Southern choice with craft beer", "Crepes & craft beer at a casual cafe/bar", "Classy setting for modern Southern eats", "Informal stop for Mediterranean fare", "Italian kitchen in an old garage space", "PBrick-oven pizza & craft beers on tap", "Craft beer & reimagined pub fare", "Guess what kind of food", "Stylish New American eatery & bar"]
var latLong = [[33.960148,-83.375147],[33.959384,-83.374598],[33.958231, -83.377229],[33.939764, -83.385928],[33.957978, -83.374932],[33.958086, -83.376514],[33.959761, -83.375207],[33.960177, -83.382644],[33.958810, -83.379817],[33.939685, -83.385965],[33.958456, -83.378970],[33.960032, -83.382812],[33.958025, -83.378849]]


@IBOutlet weak var home: UIButton!

@IBOutlet weak var myMap: MKMapView!
var myLat = 33.958810
var myLong = -83.379817

override func viewDidLoad() {
    super.viewDidLoad()

    mainScrollView.contentSize.height = 2885

    myMap.mapType = MKMapType.satellite

    let initialLocation = CLLocation(latitude: myLat, longitude: myLong)
    let regionRadius: CLLocationDistance = 1000
    func centerMapOnLocation (location:CLLocation){
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius, regionRadius)
    myMap.setRegion(coordinateRegion, animated: true)
    }
    centerMapOnLocation(location: initialLocation)
    let howMany = latLong.count
    var latLongCounter = 0
    while (latLongCounter < howMany){
        let pinLat = latLong [latLongCounter][0]
        let pinLong = latLong [latLongCounter][1]

        var pinCoordinates: CLLocationCoordinate2D = CLLocationCoordinate2D()
        pinCoordinates.latitude = CDouble(pinLat)
        pinCoordinates.longitude = CDouble(pinLong)
        let pin: MKPointAnnotation = MKPointAnnotation()
        pin.coordinate = pinCoordinates
        self.myMap.addAnnotation(pin)
        pin.title = myTitles[latLongCounter]
        pin.subtitle = mySubtitles[latLongCounter]
    latLongCounter += 1
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    //if segue.identifier == "restaurants" {
    //if let destination = segue.destination as? restaurants {
    //destination.currentSite = self.currentSite
    //}
    //}


    if segue.identifier == "button01" {
        var url = NSURL(string: siteLink[0])
        if let destination = segue.destination as? restaurants {
            destination.url = self.url
        }

        //func loadPage(){
            //var url = NSURL(string: siteLink[0])
            //let url = NSURL(string: self.url)
            //(string: siteLink[currentSite])
            //let request = NSURLRequest(url:url as! URL)
            //if let destination = segue.destination as? restaurants {
                //destination.url = self.url
            //}
            //self.eatView.loadRequest(request as URLRequest)
        //}
    }
    else if segue.identifier == "button02" {
        var url = NSURL(string: siteLink[1])
        if let destination = segue.destination as? restaurants {
            destination.url = self.url
        }

        //func loadPage(){
            //var url = NSURL(string: siteLink[1])
            //let url = NSURL(string: self.url)
            //(string: siteLink[currentSite])
            //let request = NSURLRequest(url:url as! URL)
            //if let destination = segue.destination as? restaurants {
                //destination.url = self.url
            //}
            //self.eatView.loadRequest(request as URLRequest)
        //}
    }
    else if segue.identifier == "button03" {
        var url = NSURL(string: siteLink[2])
        if let destination = segue.destination as? restaurants {
            destination.url = self.url
        }

        //func loadPage(){
            //var url = NSURL(string: siteLink[2])
            //let url = NSURL(string: self.url)
            //(string: siteLink[currentSite])
            //let request = NSURLRequest(url:url as! URL)
            //if let destination = segue.destination as? restaurants {
                //destination.url = self.url
            //}
            //self.eatView.loadRequest(request as URLRequest)
        //}
    }

}

    }

and View Controller 2 aka restaurants:

import UIKit
import MapKit

//var restaurantIndex = siteLink.count
//var urlWebsite = NSURL(string: siteLink[restaurantIndex])

//func displayURL ()
//{
//let myURLString = "http://google.com"
//let myURL = NSURL(string: myURLString)
//let myURLRequest = NSURLRequest(URL: myURL! asURL)
//webView.loadRequest(myURLRequest)
//}

class restaurants: UIViewController {

var siteLink = ["http://amici-cafe.com/athens.html", "http://www.athensbagel.com/", "https://www.facebook.com/dawggonegoodbbq/", "http://www.fuzzystacoshop.com/", "http://places.singleplatform.com/globe/menu?ref=google", "https://www.facebook.com/Kellys-Authentic-Jamaican-Food-333515583330794/", "http://theplaceathens.com/", "http://www.pauleyscrepebar.com/", "http://southkitchenbar.com/", "http://tazikiscafe.com/", "http://www.tedsmostbest.com/", "https://yourpie.com/", "http://trappezepub.com/", "http://www.vivaargentinecuisine.com/", "http://five-bar.com/bar-locations/athens-georgia/"]
var currentSite = 0
var url = String()

@IBOutlet weak var home: UIButton!

@IBOutlet weak var eatView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

   // displayURL ()
    //func loadPage(){
        let url = NSURL(string: self.url)
        //(string: siteLink[currentSite])
        let request = NSURLRequest(url:url as! URL)
        self.eatView.loadRequest(request as URLRequest)
//        }

    //nermal

    //let url = NSURL(string: "http://www.creaturecomfortsbeer.com/the-brand/")
    //let request = NSURLRequest(url:url! as URL)
    //self.webView!.loadRequest(request as URLRequest)

    //jank

    //let url = NSURL(string: siteLink[restaurantIndex])
   //let request = NSURLRequest(url:url! as URL)
    //self.webView!.loadRequest(request as URLRequest)

   // }

 }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}
}
Mary Hitchings
  • 111
  • 1
  • 5

1 Answers1

0

3 things.

You need a variable to store the selected restaurant index. Let's say restaurantIndex.

Move the webView setup to viewWillAppear():

override func viewWillAppear() {
     let url = NSURL(string: siteLink[restaurantIndex])
     let request = NSURLRequest(url:url! as URL)
     self.webView!.loadRequest(request as URLRequest)
}

Choose a method to pass restaurantIndex between views depending on how you have it set up. See: How do you share data between view controllers and other objects in Swift?

Community
  • 1
  • 1
Eggsalad
  • 383
  • 2
  • 9
  • What is the restaurantIndex storing? – Mary Hitchings Apr 30 '17 at 20:55
  • It's the index of siteLink for the url you want to use in your webView, which should be the same index as the selected data in myTitles, mySubtitles, and latLong, depending on which button was clicked. – Eggsalad Apr 30 '17 at 21:07
  • okay yep. I getcha. But I just need the urls- a different url depending on what button is clicked. – Mary Hitchings Apr 30 '17 at 22:44
  • Right, so you need to get the value of restaurantIndex in the IBAction method(s) in eatMap triggered when a button is tapped, which you don't have yet. Then you need to pass that value to the other viewController, for which I suggest referring to the other thread I mentioned. – Eggsalad Apr 30 '17 at 23:19
  • Can I get something more concrete? I've spent days on these sites trying to plug in values and decipher this syntax and I'm just stuck now. No errors at this point after probably 14 hours of labor, but the UIWebView still isn't loading the urls. Can I get an example of what my prepareforSegue should look like to pass those values? or example functions of my IBActions? – Mary Hitchings May 01 '17 at 19:24