UiWebView有几个标签

时间:2015-04-07 12:06:42

标签: swift tabs uiwebview

我做了很多搜索,但是我没有找到任何东西来帮助我创建(使用UiWebView)几个页面/标签,比如每个浏览器(例如Chrome / Safari ......)。

我正在搜索如何在Swift中创建和管理多个标签。

1 个答案:

答案 0 :(得分:0)

您可以使用TabBarController来实现此目标。请参见下面的简单说明。但是,要实现更强大的应用程序,您应该了解如何以编程方式实现tabBarController,它允许您根据计划显示的页面创建任意数量的选项卡。在下面的插图中,我使用了一个故事板,其中UIViewController分段为2标签视图。每个选项卡视图由同一WebViewController驱动。在WebViewController的viewDidAppear函数中,我使用tabBarController.selectedIndex来确定选择了哪个选项卡,并从要显示的数组中选择相应的URL。

import UIKit

class ViewController: UIViewController {
override func viewDidAppear(animated: Bool) {

    // segue to tabViewController after initial viewController becomes visible
    // wont work in viewDidLoad.
    // In robust implementation you should use a view controller array to define
    // variable number of tab controllers depending on the number of pages

    performSegueWithIdentifier("segueToTab", sender: self)
}
override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}


}

import UIKit

class WebViewController: UIViewController {
var webView: UIWebView!
var myUrls = ["http://www.google.com","http://www.yahoo.com"]
var url: NSURL!
override func viewDidAppear(animated: Bool) {
    let webView = UIWebView(frame: view.bounds)
    if let tabBarController = self.tabBarController {
        let index = tabBarController.selectedIndex
        println("selectedIndex = \(index)")
        url = NSURL(string: myUrls[index])
        let urlRequest = NSURLRequest(URL: url)
        webView.loadRequest(urlRequest)

    }

    view.addSubview(webView)

}

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}