Swift 简单控件示例:搜索框(UISearchBar)

转载请声明出处:http://www.jb51.cc/article/p-savwrnzi-ga.html
------------------------------------------------------------------------------------------

//
//  ViewController.swift
//  UISearchBarSample
//
//  Created by jinnchang on 15/4/2.
//  Copyright (c) 2015年 Jinn Chang. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UISearchBarDelegate {
    
    var searchBar: UISearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.
        
        searchBar = UISearchBar(frame: CGRectMake(0,20,self.view.frame.size.width,80))
        searchBar.placeholder = "search"
        searchBar.barStyle = .Default
        searchBar.showsBookmarkButton = true
        searchBar.showsCancelButton = true
        searchBar.delegate = self
        
        self.view.addSubview(searchBar)
    }

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

    // 每次输入内容改变触发事件
    func searchBar(searchBar: UISearchBar,textDidChange searchText: String) {
        println("筛选内容:\(searchText)")
    }
    
    // 书签按钮触发事件
    func searchBarBookmarkButtonClicked(searchBar: UISearchBar) {
        println("搜索历史记录")
    }
    
    // 取消按钮触发事件
    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        println("取消搜索")
    }
    
    // 搜索触发事件
    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        println("开始搜索")
    }

}
------------------------------------------------------------------------------------------
GitHub 上项目地址:UISearchBarSample
控件更多相关属性方法参考:UISearchBar Class Reference 文章最后更新时间:2015年4月3日11:42:05

相关文章

Swift 正式开源!Swift 团队很高兴宣布 Swift 开始开源新篇章。自从苹果发布 Swfit 编程语言,就成为了...
快,快,快!动动您的小手,分享给更多朋友! 苹果去年推出了全新的编程语言Swift,试图让iOS开发更简单...
开发者(KaiFaX) 面向开发者、程序员的专业平台! 和今年年初承诺的一样,苹果贴出了Swift语言的源码,...
本文由@Chun发表于Chun Tips :http://chun.tips/blog/2014/12/11/shi-yong-swift-gou-jian-zi-ding-yi...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...