美文网首页
add contact

add contact

作者: RUIRUI999 | 来源:发表于2016-12-05 08:34 被阅读0次

    import UIKit

    class AddContactViewController: UIViewController {

    var photoView:UIImageView!

    var nameField:UITextField!

    var phoneField:UITextField!

    var addressTextView:UITextView!

    override func viewDidLoad() {

    super.viewDidLoad()

    self.view.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)

    setNavigationItem()

    setupViews()

    }

    //配置导航栏

    func setNavigationItem() {

    self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: .done, target: self, action: #selector(cancelAction))

    self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "保存", style: .done, target: self, action: #selector(saveAction))

    }

    func setupViews() {

    //图片

    photoView = UIImageView(frame:CGRect(x: 132, y: 80, width: 150, height: 150))

    photoView.backgroundColor = #colorLiteral(red: 0.9764705896, green: 0.9154091712, blue: 0.7610167191, alpha: 1)

    photoView.clipsToBounds = true

    photoView.layer.cornerRadius = 50

    self.view.addSubview(photoView)

    //添加轻拍手势

    let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction))

    //打开交互

    photoView.isUserInteractionEnabled = true

    //photoView上添加手势

    photoView.addGestureRecognizer(tap)

    //姓名

    nameField = UITextField(frame: CGRect(x: 107, y: 240, width: 200, height: 40))

    nameField.placeholder = "请输入姓名"

    nameField.borderStyle = .roundedRect

    self.view.addSubview(nameField)

    //电话

    phoneField = UITextField(frame: CGRect(x: 107, y: 290, width: 200, height: 40))

    phoneField.placeholder = "请输入电话"

    phoneField.borderStyle = .roundedRect

    self.view.addSubview(phoneField)

    //住址

    addressTextView = UITextView(frame: CGRect(x: 107, y: 340, width: 200, height: 120))

    addressTextView.text = "地址:"

    addressTextView.layer.borderWidth = 1.0

    addressTextView.layer.borderColor = UIColor.gray.cgColor

    self.view.addSubview(addressTextView)

    }

    func cancelAction(sender:UIBarButtonItem)  {

    //        print("取消")

    self.dismiss(animated: true, completion: nil)

    }

    func saveAction(sender:UIBarButtonItem) {

    //        print("保存成功")

    //姓名或者电话号码为空则不添加

    if nameField.text?.characters.count == 0 || phoneField.text?.characters.count == 0 {

    return print("姓名或电话号码为空")

    }

    let aContact = Contact()

    aContact.name = nameField.text

    aContact.phone = phoneField.text

    aContact.address = addressTextView.text

    aContact.photo = photoView.image

    //添加联系人

    //单例调用添加联系人的方法

    ContactManger.shared.addContact(aContact: aContact)

    self.dismiss(animated: true, completion: nil)

    }

    //MARK:- 轻拍手势关联方法

    func tapAction(sender:UITapGestureRecognizer) {

    //        print("点击图片")

    //添加弹出视图sheet样式的

    let alertVC = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    //添加按钮 相机 相册 取消

    let action1 = UIAlertAction(title: "相机", style: .destructive, handler: {

    (sender:UIAlertAction) in

    //            print("选择了相机")

    //调用选择相机的方法

    self.pickerImageFromCamera()

    })

    let action2 = UIAlertAction(title: "相册", style: .destructive, handler: {

    (sender:UIAlertAction) in

    //            print("选择了相册")

    //调用选择相册的方法

    self.piskerImageFromPhotoLibrary()

    })

    let action3 = UIAlertAction(title: "取消", style: .cancel, handler: nil)

    //将按钮添加到alertVC上

    alertVC.addAction(action1)

    alertVC.addAction(action2)

    alertVC.addAction(action3)

    //模态出alertVC

    self.present(alertVC, animated: true, completion: nil)

    }

    //选择相机

    func pickerImageFromCamera() {

    }

    //选择相册

    func piskerImageFromPhotoLibrary() {

    }

    override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

    }

    /*

    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    // Get the new view controller using segue.destinationViewController.

    // Pass the selected object to the new view controller.

    }

    */

    }

    相关文章

      网友评论

          本文标题:add contact

          本文链接:https://www.haomeiwen.com/subject/cjjkmttx.html