抱歉,我使用的是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)
}
然后我使用此代码来识别按下了哪个“按钮”:
//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")
}
}
按下四个或五个按钮打印相应的消息,但按其他按钮会返回“致命错误:在展开可选值(lldb)时意外发现nil”错误。我试图通过在func tabBar中添加和删除self来解决这个问题,但没有成功。任何人都可以看到我的错误在哪里?任何帮助将不胜感激。
答案 0 :(得分:0)
好的,对于追随我的人,这是我创建的解决方案:
步骤1我从变量中删除了弱并在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 ViewController: UIViewController, UIToolbarDelegate, UITabBarDelegate, UIWebViewDelegate{