// // SearchViewController.swift // SearchDemo // // Created by Ralf Gronkowski on 13/10/14. // Copyright (c) 2014 Ralf Gronkowski. All rights reserved. // import UIKit protocol SearchViewControllerDelegate { func didFinishTypingText(typedText: String?) } class SearchViewController: UIViewController, UITextFieldDelegate { var textField: UITextField? var newItemText: String? var delegate: SearchViewControllerDelegate? override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.whiteColor() setupTextField() setupNavigationBar() } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) textField?.becomeFirstResponder() } func setupTextField() { textField = UITextField(frame: CGRectZero) textField?.placeholder = "Type in search query" textField?.delegate = self view.addSubview(textField!) } func setupNavigationBar() { navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "searchAction") let aBarButton = UIBarButtonItem(title: "a", style: UIBarButtonItemStyle.Bordered, target: self, action: "aAction") let bBarButton = UIBarButtonItem(title: "b", style: UIBarButtonItemStyle.Bordered, target: self, action: "bAction") let cBarButton = UIBarButtonItem(title: "c", style: UIBarButtonItemStyle.Bordered, target: self, action: "cAction") navigationItem.leftBarButtonItems = [aBarButton,bBarButton,cBarButton] } override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() let padding = CGFloat(11) textField?.frame = CGRectMake(padding, self.topLayoutGuide.length + 50, view.frame.size.width - padding * 2, 100) } func searchAction() { delegate?.didFinishTypingText(textField?.text) dismissViewControllerAnimated(true, completion: nil) } func aAction() { textField?.text = "l:p4attr_tags:" } func bAction() { textField?.text = "l:filename:" } func cAction() { textField?.text = "l:content_type:image/jpeg AND aperture:[1.4 TO 9] AND p4attr_tags:angry" } func textFieldShouldReturn(textField: UITextField!) -> Bool { searchAction() textField.resignFirstResponder() return true } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 10709 | gronk |
First working example of an iOS app that searches in P4Search and displys content by retrieving it from Swarm. There is a file JSON.swift in the project in which you need to paste some external code from here https://github.com/lingoer/SwiftyJSON/blob/master/SwiftyJSON/SwiftyJSON.swift to simplify JSON parsing. You will also need access to a running P4Search and Swarm instance if you want to run the app on a device or inside an emulator. Coded in Swift with XCode 6.0.1 |