Files
libr-ios/librx/CreatePostViewController.swift
2015-11-30 23:02:06 +08:00

80 lines
2.6 KiB
Swift

//
// CreatePostViewController.swift
// librx
//
// Created by Yunlong Zheng on 15/8/31.
// Copyright (c) 2015年 librx. All rights reserved.
//
import UIKit
import Toast
class CreatePostViewController: UITableViewController {
@IBOutlet weak var share_link: UITextField!
@IBOutlet weak var share_description: UITextField!
@IBOutlet weak var type: UILabel!
private var shareType: String = "article"
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
}
@IBAction func switchValueChange(sender: AnyObject) {
let withSwicth = sender as! UISwitch
let setting = withSwicth.on
if setting {
self.type.text = "文章"
self.shareType = "article"
} else {
self.type.text = "网页"
self.shareType = "page"
}
}
@IBAction func shareButtonDidPressed(sender: AnyObject){
_ = self.share_description?.text
let link = self.share_link?.text
NSURL.validateUrl(link) { [weak self](success, urlString, error) -> Void in
if let _ = self {
if success {
let post = Post(type: self!.shareType, url: link!)
LibrxService.createPost(post) { [weak self](json) -> () in
if let selfView = self {
if let msg = json["msg"].string {
selfView.alert(msg)
}else {
selfView.view.makeToast("分享成功", duration:3.0, position: CSToastPositionTop)
NSTimer.scheduledTimerWithTimeInterval(2.0, target: selfView, selector: Selector("dismiss"), userInfo: nil, repeats: false)
}
}}
}else {
self?.view.makeToast("请输入正确的链接地址", duration:2.0, position: CSToastPositionTop)
}
}
}
}
func dismiss() {
self.dismissViewControllerAnimated(true, completion: nil)
}
func alert(message: String) {
let alc = UIAlertController(title: message, message: nil, preferredStyle: UIAlertControllerStyle.Alert)
alc.addAction(UIAlertAction(title: "确定", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(alc, animated: true, completion: nil)
}
}