多个按钮:单击时,将url从数组传递到2nd View Controller的UIwebview

时间:2017-04-30 20:01:27

标签: ios arrays swift uiwebview segue

我有两个视图控制器涉及此问题。第一个是“eatMap”,第二个是“餐馆”。 eatMap包含多个按钮,用于显示餐馆的图片和名称,以及包含每个位置的图钉的地图。我希望餐厅View Controller在名为“eatView”的UIWebView中显示每个餐厅的菜单。我有一个数组持有每个网站的网址。

如何根据点击的按钮指示UIWebView加载不同的网址?

我怀疑问题出在我的prepareForSegue函数或我的var currentSite或var url中,但我不确定。我知道网址没有正确传递到UIWebView。

我被困住了!

抱歉所有的反​​斜杠。我一直在努力追踪我曾经尝试过的让我在任何地方找到的东西。

这里是View Controller 1又名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)
        //}
    }

}

    }

和View Controller 2又名餐馆:

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()

}
}

1 个答案:

答案 0 :(得分:0)

3件事。

您需要一个变量来存储所选的餐厅索引。我们先说restaurantIndex

将webView设置移至viewWillAppear()

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

选择一种方法,根据您设置的方式在视图之间传递restaurantIndex。请参阅:How do you share data between view controllers and other objects in Swift?