add contact

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.

}

*/

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容