Difference between revisions of "Swift code snippets"

From Hawk Wiki
Jump to: navigation, search
m
(Navigate to Storyboard ID)
Line 53: Line 53:
 
  tableView.rowHeight = UITableViewAutomaticDimension
 
  tableView.rowHeight = UITableViewAutomaticDimension
 
</pre>
 
</pre>
==Navigate to Storyboard ID==
+
==Navigation between storyboards==
 +
===Navigate to Storyboard ID===
 
<pre class="brush:swift">
 
<pre class="brush:swift">
 
var vc = storyboard.instantiateViewControllerWithIdentifier("loginViewController") as UIViewController
 
var vc = storyboard.instantiateViewControllerWithIdentifier("loginViewController") as UIViewController
 
window?.rootViewController = vc
 
window?.rootViewController = vc
 +
</pre>
 +
===Navigate Segue by Identifier===
 +
First, drag a segue in storyboard and give it an identifier, then in the button action (or any action you like), do:
 +
<pre class="brush:swift">
 +
self.performSegueWithIdentifier("loginSegue", sender: self)
 +
</pre>
 +
===Get UIVewController from Navigation Controller===
 +
Sometimes, you want to access the UIViewController from a navigation controller
 +
<pre class="brush:swift">
 +
var vc = storyboard?.instantiateViewControllerWithIdentifier("navigationID") as UINavigationController
 +
var v = vc.viewControllers[0] as ViewController // [0] represents the first level UIViewController under NavigationController
 
</pre>
 
</pre>

Revision as of 02:57, 18 March 2015

Swift Code snippts

Back To IOS_Swift

Making Network Request

var clientId = "Put your client id here"
 
var url = NSURL(string: "https://api.instagram.com/v1/media/popular?client_id=\(clientId)")!
var request = NSURLRequest(URL: url)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
    var responseDictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as NSDictionary
    self.photos = responseDictionary["data"] as NSArray
    self.tableView.reloadData()
    
    println("response: \(self.photos)")
}

Use AFNetworking setImageWithURL

Create the bridging file: create a new Objective-C file, name it anything, and when you save it, Xcode will prompt you generate a bridging file.
In an Objective-C bridging file:

#import <AFNetworking/UIImageView+AFNetworking.h>

In the swift view file, to set a imageView using URL:

setImageWithURL(NSURL(string: thumbnailURL))

Adding UIRefreshControl

//refreshControl is automatically defined in UITableViewController
var refreshControl: UIRefreshControl?
override func viewDidLoad() {
    super.viewDidLoad()
    
    refreshControl = UIRefreshControl()
    refreshControl?.addTarget(self, action: "fetch", forControlEvents: UIControlEvents.ValueChanged)
    tableView.insertSubview(refreshControl!, atIndex: 0)
}

//to stop refresh, call
if let rc = self.refreshControl {
    self.refreshControl!.endRefreshing()
}

Example of fetching messages from Parse

https://gist.github.com/sandofsky/7134b1ff90d235901254

Auto Table Row Height

//Auto table row height
 tableView.estimatedRowHeight = 92.0
 tableView.rowHeight = UITableViewAutomaticDimension

Navigation between storyboards

Navigate to Storyboard ID

var vc = storyboard.instantiateViewControllerWithIdentifier("loginViewController") as UIViewController
window?.rootViewController = vc

Navigate Segue by Identifier

First, drag a segue in storyboard and give it an identifier, then in the button action (or any action you like), do:

self.performSegueWithIdentifier("loginSegue", sender: self)

Get UIVewController from Navigation Controller

Sometimes, you want to access the UIViewController from a navigation controller

var vc = storyboard?.instantiateViewControllerWithIdentifier("navigationID") as UINavigationController
var v = vc.viewControllers[0] as ViewController // [0] represents the first level UIViewController under NavigationController