这两个用法比较简单:
具体代码如下:
一、UISegmentedControl
1、UISegmentedControl的声明
var segment = UISegmentedControl()
2、UISegmentedControl的创建
func initSegment() { segment = UISegmentedControl(items:["one", "two", "three", "four"]) segment.frame = CGRect(x:10.0, y:100.0, SCREEN_WIDTH - 20.0, height:30.0) segment.isMomentary = true segment.selectedSegmentIndex = 0 //设置选中的是第一个选项 segment.addTarget(self, action: action: #selector(segClick(segTapped:)), forControlEvents: .touchUpInside) self.view.addSubview(segment) }
3、响应方法的实现
func segClick(segTapped:UISegmentedControl) { print("index is (sender.selectedSegmentIndex)") }
UIWebView
1、声明
var webView = UIWebView()
2、创建
func initWebView() { webView.frame = CGRect(x:0, y:220, SCREEN_WIDTH, height:SCREEN_HEIGHT - 220) webView.backgroundColor = UIColor.blue let url = NSURL(string: "https://www.baidu.com/") let request = NSURLRequest(URL: url! as URL) webView.loadRequest(request) self.view.addSubview(webView) }
3、webview响应的代理方法
这里就不一一列举,可以自己点进去看看,常用的就是那几个监听的方法,如下:
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool
func webViewDidStartLoad(webView: UIWebView)
func webViewDidFinishLoad(webView: UIWebView)
func webView(webView: UIWebView, didFailLoadWithError error: NSError?)