「Ios/swift/SearchBar」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「==SearchBarとは== 検索ボックスです ==検索バーサンプル== import UIKit class SearchViewController: UIViewController, UISearchBarDelegate { var mySe...」) |
|||
(同じ利用者による、間の7版が非表示) | |||
行6: | 行6: | ||
class SearchViewController: UIViewController, UISearchBarDelegate { | class SearchViewController: UIViewController, UISearchBarDelegate { | ||
var mySearchBar: UISearchBar! | var mySearchBar: UISearchBar! | ||
+ | private var _searchText: String = "" | ||
override func viewDidLoad() { | override func viewDidLoad() { | ||
super.viewDidLoad() | super.viewDidLoad() | ||
mainSearchBar.delegate = self | mainSearchBar.delegate = self | ||
+ | mainSearchBar.placeholder = "検索" | ||
} | } | ||
− | + | // 検索バー更新時 | |
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { | func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { | ||
− | + | _searchText = searchText | |
} | } | ||
// キャンセル | // キャンセル | ||
func searchBarCancelButtonClicked(searchBar: UISearchBar) { | func searchBarCancelButtonClicked(searchBar: UISearchBar) { | ||
− | + | _searchText = "" | |
− | + | ||
} | } | ||
// 検索ボタン | // 検索ボタン | ||
func searchBarSearchButtonClicked(searchBar: UISearchBar) { | func searchBarSearchButtonClicked(searchBar: UISearchBar) { | ||
− | + | mainSearchBar.text = "" | |
− | + | ||
self.view.endEditing(true) | self.view.endEditing(true) | ||
+ | // searchText | ||
} | } | ||
} | } | ||
==SearchBarをコードで生成する場合== | ==SearchBarをコードで生成する場合== | ||
− | + | mainSearchBar = UISearchBar() | |
− | + | mainSearchBar.delegate = self | |
− | + | mainSearchBar.frame = CGRectMake(0, 0, 300, 50) | |
− | + | mainSearchBar.layer.position = CGPoint(x: self.view.bounds.width/2, y: 50) | |
− | + | mainSearchBar.showsCancelButton = true | |
− | + | mainSearchBar.placeholder = "検索" | |
− | + | mainSearchBar.text = "テスト" | |
+ | self.view.addSubview(mainSearchBar) |
2016年7月31日 (日) 12:15時点における最新版
SearchBarとは
検索ボックスです
検索バーサンプル
import UIKit class SearchViewController: UIViewController, UISearchBarDelegate { var mySearchBar: UISearchBar! private var _searchText: String = "" override func viewDidLoad() { super.viewDidLoad() mainSearchBar.delegate = self mainSearchBar.placeholder = "検索" } // 検索バー更新時 func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { _searchText = searchText } // キャンセル func searchBarCancelButtonClicked(searchBar: UISearchBar) { _searchText = "" } // 検索ボタン func searchBarSearchButtonClicked(searchBar: UISearchBar) { mainSearchBar.text = "" self.view.endEditing(true) // searchText } }
SearchBarをコードで生成する場合
mainSearchBar = UISearchBar() mainSearchBar.delegate = self mainSearchBar.frame = CGRectMake(0, 0, 300, 50) mainSearchBar.layer.position = CGPoint(x: self.view.bounds.width/2, y: 50) mainSearchBar.showsCancelButton = true mainSearchBar.placeholder = "検索" mainSearchBar.text = "テスト" self.view.addSubview(mainSearchBar)