Skip to content

Instantly share code, notes, and snippets.

@v5tech
Last active August 29, 2015 14:02
Show Gist options
  • Save v5tech/9d1a89b1a38742ffe210 to your computer and use it in GitHub Desktop.
Save v5tech/9d1a89b1a38742ffe210 to your computer and use it in GitHub Desktop.
Swift Code 碎片整理

Swift Code 碎片整理

1. NSURLConnection

var url = NSURL(string: "http://www.stackoverflow.com")
var request = NSURLRequest(URL: url)

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) in
        println(NSString(data: data, encoding: NSUTF8StringEncoding))
})

2. UIAlertView

let alertView:UIAlertView = UIAlertView()
alertView.title="提示"
alertView.message="恭喜你,你中奖了!"
alertView.addButtonWithTitle("确定")
alertView.show()

或者

var alert:UIAlertController = UIAlertController(title: "提示", message: "恭喜你,你中奖了!", preferredStyle: UIAlertControllerStyle.Alert)
var action: UIAlertAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler: {
    ac -> Void in
})
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)

3. navigationItem

//设置导航栏按钮(左边)
let leftBarButtonItem:UIBarButtonItem = UIBarButtonItem(title: "主页", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("showMain"))
self.navigationItem.leftBarButtonItem = leftBarButtonItem
        
//设置导航栏按钮(右边)
let rightBarButtonItem:UIBarButtonItem = UIBarButtonItem(title: "设置", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("showSetting"))
self.navigationItem.rightBarButtonItem = rightBarButtonItem
        
//设置导航栏名称
self.navigationItem.title="全国主要城市PM2.5"



func showMain(){

//SCLAlertView().showTitle(self,  title: "提示", subTitle: "你点击了主页", duration: 2.0, style: SCLAlertViewStyle.Info)
        
var alert = UIAlertController(title: "提示", message: "你点击了主页", preferredStyle: UIAlertControllerStyle.Alert)

alert.addAction(UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler: {
    
    (UIAlertAction) -> Void in
    
        println("你点击了确定按钮")
    
    }))

self.presentViewController(alert, animated: true, completion: nil)

}

func showSetting(){
//SCLAlertView().showTitle(self,  title: "提示", subTitle: "你点击了设置", duration: 2.0, style: SCLAlertViewStyle.Info)

var alert = UIAlertController(title: "提示", message: "你点击了设置", preferredStyle: UIAlertControllerStyle.Alert)

alert.addAction(UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler: {
    
    (UIAlertAction) -> Void in

        println("你点击了确定按钮")
    
    }))

self.presentViewController(alert, animated: true, completion: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment