点击搜索栏不提示键盘(iOS Swift)

时间:2014-11-01 00:01:03

标签: ios xcode swift uisearchbar

我是一个Swift noob,很抱歉这个简单的问题。

我认为我没有为搜索栏附加正确的事件,因为当我点击它时,键盘不会出现。

在故事板中,我非常确定我必须将UISearchBar委托的出口设置为View Controller。但是从这个屏幕截图中,看起来委托只是viewthis screenshot

这是我的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.
    }

}

1 个答案:

答案 0 :(得分:1)

你走在正确的轨道上Tim - 你似乎以某种方式将你的View设置为你的搜索栏的代表而不是View Controller。我建议:

  1. 删除Connections检查器中的连接。
  2. 在Interface Builder中打开文档大纲(界面构建器左下角的按钮)
  3. 现在更清楚的是你正在拖动什么 - 控制从搜索栏直接拖动到ViewController,选择委托,你应该看到它在Connections Inspector中正确显示。