从以编程方式生成的 UITabBar 更改以编程方式生成的 uiwebview 的内容

Changing a programmatically generated uiwebview's content from a programmatically generated UITabBar

抱歉,我正在使用 Swift 2,但我还是个菜鸟。我已经能够使用以下代码以编程方式创建 UITabBar 和 UIWebView:

class ViewController: UIViewController, UIToolbarDelegate, UITabBarDelegate {

@IBOutlet var myWebView: UIWebView!
@IBOutlet weak var tabbar: UITabBar!
//    @IBOutlet weak var myWebView:UIWebView! = UIWebView(frame: CGRectMake(0, 60, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height - 112))

override func viewDidLoad() {
    super.viewDidLoad()

//Add Webview
    let myWebView:UIWebView = UIWebView(frame: CGRectMake(0, 60, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height - 112))
    myWebView.loadRequest(NSURLRequest(URL: NSBundle.mainBundle().URLForResource ("index", withExtension: "htm")!))
    self.view.addSubview(myWebView)

//Add Toolbar for menu
    let toolbar = UIToolbar()
    toolbar.frame = CGRectMake(0, 20, self.view.frame.size.width, 46)
    toolbar.sizeToFit()
    // make a button
    let menuButton: UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "list-fat-7.png"), style:.Plain, target: self, action: "twitter")
    menuButton.tag = 1
    toolbar.items = [menuButton]
    //set toolbar color toolbar.backgroundColor = UIColor.redColor()
    self.view.addSubview(toolbar)

//Add Tab Bar
    let tabBar = UITabBar()
    tabBar.delegate = self
    tabBar.frame = CGRectMake(0, self.view.frame.size.height - 52, self.view.frame.size.width, 52)
    tabBar.sizeToFit()

    // Tab Bar buttons
    let sermonButton = UITabBarItem(title: "Sermons", image: UIImage(named: "video-camera-7.png"), selectedImage: UIImage(named: "video-camera-7.png"))
    sermonButton.tag = 1
    let bibleButton = UITabBarItem(title: "Bible", image: UIImage(named: "book-cover-7.png"), selectedImage: UIImage(named: "book-cover-7.png"))
    bibleButton.tag = 2
    let calendarButton = UITabBarItem(title: "Calendar", image: UIImage(named: "calendar-7.png"), selectedImage: UIImage(named: "calendar-7.png"))
    calendarButton.tag = 3
    let givingButton = UITabBarItem(title: "Giving", image: UIImage(named: "gift-7.png"), selectedImage: UIImage(named: "gift-7.png"))
    givingButton.tag = 4
    let bulletinButton = UITabBarItem(title: "Bulletin", image: UIImage(named: "newspaper-7.png"), selectedImage: UIImage(named: "newspaper-7.png"))
    bulletinButton.tag = 5
    tabBar.items = [sermonButton,bibleButton,calendarButton,givingButton,bulletinButton]
    self.view.addSubview(tabBar)
}

然后我使用此代码来识别按下了哪个 "button":

    //Mapping Tab Bar Buttons
    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
    if item.tag == 1 {
        self.myWebView.loadRequest(NSURLRequest(URL: NSURL(string:"https://www.ustream.tv/combined-embed/10786920?social=0&videos=gallery&videosCount=4&html5ui")!))
    } else if item.tag == 2 {
        self.myWebView.loadRequest(NSURLRequest(URL: NSBundle.mainBundle().URLForResource ("index", withExtension: "htm")!))
    } else if item.tag == 3 {
        self.myWebView.loadRequest(NSURLRequest(URL: NSBundle.mainBundle().URLForResource ("Agenda", withExtension: "html")!))
    } else if item.tag == 4 {
            print("ApplePay")
    } else if item.tag == 5 {
            print("Bulletin")
    }

}

按第四个或第五个按钮会打印相应的消息,但按其他按钮 returns 会出现 "fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)" 错误。我试图通过在 func tabBar 中添加和删除 self 来解决这个问题,但没有成功。谁能看到我的错误在哪里?任何帮助将不胜感激。

好的,对于那些关注我的人,这是我创建的解决方案:

第 1 步我从变量中删除了 weak 并在 IBOutlet 中创建了框架:

@IBOutlet var myWebView:UIWebView! = UIWebView(frame: CGRectMake(0, 60, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height - 112))

第 2 步我使用了“!”解决我的展开错误:

myWebView!.loadRequest(NSURLRequest(URL:... 

第 3 步我将委托包含在 class 声明中:

class ViewController: UIViewController, UIToolbarDelegate, UITabBarDelegate, UIWebViewDelegate{