0
我是一個Swift noob,所以對於簡單的問題抱歉。點擊搜索欄不提示鍵盤(iOS Swift)
我想我沒有爲我的搜索欄附加正確的事件,因爲當我點擊它時,鍵盤沒有出現。
在故事板中,我非常肯定我必須爲UISearchBar的代理設置視圖控制器。但從這張截圖看來,代表只是view
。
這裏是我的viewController.swift文件,有人可以幫助發現錯誤?
//
// ViewController.swift
// PhotoSearchExample
//
// Created by Tim Peterson on 10/30/14.
// Copyright (c) 2014 Tim Peterson. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UISearchBarDelegate {
@IBOutlet weak var scrollView: UIScrollView!
//@IBOutlet weak var searchString: UISearchBar!
let instagramClientID = "xxxx"
func searchInstagramByHashtag(searchString: String) {
for subview in self.scrollView.subviews {
subview.removeFromSuperview()
}
let instagramURLString = "https://api.instagram.com/v1/tags/" + searchString + "/media/recent?client_id=" + instagramClientID
let manager = AFHTTPRequestOperationManager()
self.scrollView.alpha = 0.0
let activityIndictorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
activityIndictorView.center = self.view.center
self.view.addSubview(activityIndictorView)
activityIndictorView.startAnimating()
manager.GET(instagramURLString,
parameters: nil,
success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println("JSON: " + responseObject.description)
if let dataArray = responseObject.valueForKey("data") as? [AnyObject] {
self.scrollView.contentSize = CGSizeMake(320, CGFloat(320*dataArray.count))
for var i = 0; i < dataArray.count; i++ {
let dataObject: AnyObject = dataArray[i]
if let imageURLString = dataObject.valueForKeyPath("images.standard_resolution.url") as? String {
println("image " + String(i) + " URL is " + imageURLString)
let imageView = UIImageView(frame: CGRectMake(0, CGFloat(320*i), 320, 320))
self.scrollView.addSubview(imageView)
imageView.setImageWithURL(NSURL(string: imageURLString))
activityIndictorView.stopAnimating()
UIView.animateWithDuration(1.0, animations: {
self.scrollView.alpha = 1.0
}, completion: {
(value: Bool) in
println("Animation complete!")
})
}
}
}
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Error: " + error.localizedDescription)
})
}
func searchBarSearchButtonClicked(searchBar: UISearchBar!) {
//println("search bar clicked!")
searchBar.resignFirstResponder()
searchInstagramByHashtag(searchBar.text)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
searchInstagramByHashtag("clararockmore")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
這是完美的,謝謝!好的搜索功能現在可以正常工作,但鍵盤從不出現。 'searchBar.resignFirstResponder()'在正確的位置? – 2014-11-01 00:34:24
如果您說鍵盤從不出現,搜索功能如何工作?哦 - 你可能在模擬器中,用你的電腦鍵盤輸入搜索。如果是這樣,請在模擬器菜單中關閉「硬件」,「鍵盤」,「連接硬件鍵盤」 - 或者只是在設備上查看它! – 2014-11-01 23:14:07
是的,我在模擬器中。再次感謝您的答案! – 2014-11-01 23:35:49