Requirements:
1.Xcode6 or Later
2.Mac Osx 10.10 Later
Development:
Variable Declaration in ios Swift
Local Variable
1.Xcode6 or Later
2.Mac Osx 10.10 Later
Development:
Variable Declaration in ios Swift
Local Variable
var engine : NSString = ""
Global Variable
var engine : NSString?
UIAlertController
var alert = UIAlertController(title: "Alert", message:"welcome" as String , preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
Creating Method:
func searchItunesFor(searchTerm: String)
{
Print(searchTerm) //This is print log or console
}
Where
searchItunesFor-Method Name
searchTerm: -receiver Parameter
String -Type of Parameter
Method Calling
searchItunesFor("JQ Software")
Array and Dictionary
var tableData: NSArray = []
var rowData: NSDictionary= self.tableData[indexPath.row] as NSDictionary
Json Request
func searchItunesFor(searchTerm: String)
{
// The iTunes API wants multiple terms separated by + symbols, so replace spaces with + signs
var itunesSearchTerm = searchTerm.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
// Now escape anything else that isn't URL-friendly
var escapedSearchTerm = itunesSearchTerm.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
var urlPath = "https://itunes.apple.com/search?term=\(escapedSearchTerm)&media=software"
var url: NSURL = NSURL(string: urlPath)
var session = NSURLSession.sharedSession()
var task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
println("Task completed")
if(error) {
// If there is an error in the web request, print it to the console
println(error.localizedDescription)
}
var err: NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
if(err?) {
// If there is an error parsing JSON, print it to the console
println("JSON Error \(err!.localizedDescription)")
}
// println(jsonResult)
var results: NSArray = jsonResult["results"] as NSArray
dispatch_async(dispatch_get_main_queue(), {
self.tableData = results
self.appsTableView.reloadData()
})
})
task.resume()
}
UITableView Delegate And DataSource
//Number of Row in tableView
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
return tableData.count
}
// cellForRowAtIndexPath in tableView
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
var rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
cell.text = rowData["trackName"] as String
// Grab the artworkUrl60 key to get an image URL for the app's thumbnail
var urlString: NSString = rowData["artworkUrl60"] as NSString
var imgURL: NSURL = NSURL(string: urlString)
// Download an NSData representation of the image at the URL
var imgData: NSData = NSData(contentsOfURL: imgURL)
cell.image = UIImage(data: imgData)
// Get the formatted price string for display in the subtitle
var formattedPrice: NSString = rowData["formattedPrice"] as NSString
cell.detailTextLabel.text = formattedPrice
return cell
}
// didSelectRowAtIndexPath in tableView
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath:
NSIndexPath!)
{
tableView.deselectRowAtIndexPath(indexPath, animated: false)
var rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
}
func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool
{
return true
}
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
// handle delete (by removing the data from your array and updating the tableview)
}
}
// heightForRow in tableView
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat
{
return 70.0;
}
navigationController
ie Left Bar Button and Right Bar Button
Left Bar Button
navigationController.setNavigationBarHidden(false, animated:true)
var myBackButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
myBackButton.addTarget(self, action: "popToRoot:", forControlEvents: UIControlEvents.TouchUpInside)
myBackButton.setTitle("Back", forState: UIControlState.Normal)
myBackButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
myBackButton.sizeToFit()
var myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
self.navigationItem.leftBarButtonItem = myCustomBackButtonItem
Right Bar Button
var myRightButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
myRightButton.addTarget(self, action: "numberTapped:", forControlEvents: UIControlEvents.TouchUpInside)
myRightButton.setTitle("Alert", forState: UIControlState.Normal)
myRightButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
myRightButton.sizeToFit()
var myCustomRightButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myRightButton)
self.navigationItem.rightBarButtonItem = myCustomRightButtonItem
func popToRoot(sender:UIBarButtonItem)
{
self.navigationController.popToRootViewControllerAnimated(true)
// self.dismissModalViewControllerAnimated(true)
}
@IBAction func numberTapped(sender: AnyObject)
{
var alert = UIAlertController(title: "Alert", message:"welcome" as String , preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
UIWebview Load URL
@IBOutlet var appsWebView : UIWebView
appsWebView.frame=CGRectMake(0, 150, self.view.bounds.width, self.view.bounds.height-150)
let url = NSURL(string: "http://www.google.com")
let request = NSURLRequest(URL: url)
appsWebView.loadRequest(request)
appsWebView.sizeToFit()
appsWebView.setNeedsDisplay()
appsWebView.setNeedsLayout()
One class to another class move
presentViewController
One class to another class move
let mainStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let vc :ViewController1 = mainStoryboard.instantiateViewControllerWithIdentifier("SecondViewController") as ViewController1
navigationController
self.navigationController.pushViewController(vc, animated: true)
self.presentViewController(navigationController, animated: true, completion: nil)