Skip to content

Instantly share code, notes, and snippets.

@honkimi
Last active August 29, 2015 14:08
Show Gist options
  • Save honkimi/c46230d30dc63dbd4ebc to your computer and use it in GitHub Desktop.
Save honkimi/c46230d30dc63dbd4ebc to your computer and use it in GitHub Desktop.
var intStr = "123" // "str"
var myInt:Int? = intStr.toInt() //nilが入ることもある
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var identifier = segue.identifier
var destVc = (segue.destinationViewController as PresentedViewController)
destVc.data = "some data from presented vc: \(identifier)"
}
// returns the number of columns to display
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 2
}
// returns the # of rows in each component.
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 10
}
// returns the title of each row
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return "(\(component),\(row))"
}
// on selected event
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
println("\(self.mPickerView.selectedRowInComponent(0))")
println("\(self.mPickerView.selectedRowInComponent(1))")
println("\(component), \(row)")
}
func showWebPage(#url: String) {
var req = NSURLRequest(URL: NSURL(string: url)!)
self.mVewbView.loadRequest(req)
}
func showWebPage(#htmlString: String) {
self.mVewbView.loadHTMLString(htmlString, baseURL: nil)
}
class ... UIWebViewDelegate {
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
// do something, like re-direct or intercept etc.
return true;
}
func webViewDidStartLoad(webView: UIWebView) {
// do something, e.g., start UIActivityViewIndicator
self.mActivityIndivator.startAnimating()
}
func webViewDidFinishLoad(webView: UIWebView) {
self.mActivityIndivator.stopAnimating()
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError) {
self.mActivityIndivator.stopAnimating()
var alert = UIAlertController(title: "error", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
let STORAGE_KEY = "key"
let userDefaults = NSUserDefaults.standardUserDefaults()
// UserDefault
func saveUserdefault(data: AnyObject, forKey: String) {
userDefaults.setObject(data, forKey: forKey)
userDefaults.synchronize()
}
func retrieveUserdefault(key: String) -> String? {
var obj = userDefaults.stringForKey(key)
return obj
}
func deleteUserDefault(key: String) {
self.userDefaults.removeObjectForKey(key)
}
var data: String?
override func viewDidLoad() {
if let tmp = data {
println("received data: \(tmp)")
}
}
import UIKit
class MasterTableViewController: UITableViewController {
var items = ["item1", "item2", "item3"]
// テーブル数のカウント. 初期化
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
// 各行のViewをセット
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("mycell") as UITableViewCell
cell.textLabel.text = self.items[indexPath.row]
return cell
}
// 各業をタップした際の挙動
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("detail", sender: indexPath.row)
}
// Detail へ遷移するSegueの定義
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var destVC = segue.destinationViewController as UIViewController
destVC.navigationItem.title = self.items[sender as Int]
}
// Bar Button Item 選択時のIBAction
@IBAction func doAdd(sender: AnyObject) {
self.items.append("item\(self.items.count + 1)")
self.tableView.reloadData()
}
}
var alert = UIAlertController(title:"My title", message: "My message", preferredStyle: UIAlertControllerStyle.Alert)
var actionCancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: {action in
// do nothing
})
var actionOk = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {action in
println((alert.textFields![0] as UITextField).text)
})
alert.addAction(actionCancel)
alert.addAction(actionOk)
alert.addTextFieldWithConfigurationHandler({textField in
textField.backgroundColor = UIColor.yellowColor()
textField.placeholder = "enter text, i.e., Do Ra Me"
})
self.presentViewController(alert,animated: true, completion: nil)
self.performSegueWithIdentifier("mypopover", sender: nil)
"app_name"="Hello iPhone";
NSLocalizedString("app_name", comment: "")
class ViewController: UIViewController, UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
var actionSheet = UIAlertController(title: "Action (from bar button item)", message: "Choose an Action", preferredStyle: UIAlertControllerStyle.ActionSheet)
var actionCancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: {action in
//nothing
})
var actionNormal1 = UIAlertAction(title: "Action 1", style: UIAlertActionStyle.Default, handler: {action in
self.logText("actionNormal1")
})
var actionDestruct = UIAlertAction(title: "Destruct", style: UIAlertActionStyle.Destructive, handler: {action in
self.logText("actionDestruct")
})
actionSheet.addAction(actionCancel)
actionSheet.addAction(actionNormal1)
actionSheet.addAction(actionDestruct)
self.presentViewController(actionSheet, animated: true, completion: nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment