我使用swift xcode 8.1创建了一个简单的应用程序 在应用程序中我在viewcotroller被解雇时应用该函数,然后在前一个viewcontroller上的UILabel.text被更改。 但总是一样的。 我应该怎么做,或者我的应用程序如何像我的计划一样工作。 谢谢
import UIKit
import RealmSwift
protocol SetButtonUnitDelegate: class{
func setUnit(numButton: Int,id: Int)
}
var inCategory = Int()
var inUnit = Int()
class ConvertViewController: UIViewController {
static var unitData = DBManager.realm.objects(UnitModel.self).filter("catId == \(inCategory)").sorted(byKeyPath: "sortOrder", ascending: true)
static var idUnit: Int = 0
@IBOutlet weak var unit1Button: UIButton!
@IBOutlet weak var unitDsc1Lbl: UILabel!
@IBOutlet weak var unit2Button: UIButton!
@IBOutlet weak var padCollectionView: UICollectionView!
@IBOutlet weak var inputNumberLbl: UILabel!
@IBOutlet weak var unitDsc2Lbl: UILabel!
let unitId = 1
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
setKeypad()
setDefaultUnit()
}
//MARK: - Layout
func setKeypad() {
let weidth = padCollectionView.frame.width
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let marginRL: CGFloat = 10
let minspacing: CGFloat = 1.0
let cellWeidth: CGFloat = (weidth / 4.0) - (minspacing + marginRL )
let cellSIze = CGSize(width: cellWeidth, height: cellWeidth)
layout.itemSize = cellSIze
layout.minimumLineSpacing = minspacing
layout.minimumInteritemSpacing = 1.0
layout.sectionInset = UIEdgeInsets(top: marginRL, left: marginRL, bottom: 1.0, right: marginRL)
padCollectionView.setCollectionViewLayout(layout, animated: true)
padCollectionView.reloadData()
}
func setDefaultUnit() {
if inButton != 0 && inButton == 1 {
print("buton pertama")
}else if inButton != 0 && inButton == 2 {
print("button ke dua")
}else {
unit1Button.setTitle(ConvertViewController.unitData[0].symbol, for: .normal)
unitDsc1Lbl.text = ConvertViewController.unitData[0].unitName
unit2Button.setTitle(ConvertViewController.unitData[1].symbol, for: .normal)
unitDsc2Lbl.text = ConvertViewController.unitData[1].unitName
print("buton awal")
}
}
//MARK: - ACtion
@IBAction func unit1Touch(_ sender: UIButton) {
showSubCat()
inButton = 1
}
@IBAction func unit2Touch(_ sender: UIButton) {
showSubCat()
inButton = 2
}
func showSubCat() {
let SVC = self.storyboard?.instantiateViewController(withIdentifier: "subCategoryTVC") as! SubCategoryTVC
let navigation = UINavigationController(rootViewController: SVC)
SVC.title = self.title
self.present(navigation, animated: true, completion: nil)
}
@IBAction func keypadOnTamped(_ sender: KeypadUIButton) {
let num: Int = (sender.indexpath?.row)!
let curent = inputNumberLbl.text!
switch num {
case 0:
if curent == "0" {
inputNumberLbl.text = "7"
}else{
inputNumberLbl.text = "\(curent)7"
}
break
case 1:
if curent == "0" {
inputNumberLbl.text = "8"
}else{
inputNumberLbl.text = "\(curent)8"
}
break
case 2:
if curent == "0" {
inputNumberLbl.text = "9"
}else{
inputNumberLbl.text = "\(curent)9"
}
break
case 3:
inputNumberLbl.text = "0"
break
case 4:
if curent == "0" {
inputNumberLbl.text = "4"
}else{
inputNumberLbl.text = "\(curent)4"
}
case 5:
if curent == "0" {
inputNumberLbl.text = "5"
}else{
inputNumberLbl.text = "\(curent)5"
}
break
case 6:
if curent == "0" {
inputNumberLbl.text = "6"
}else{
inputNumberLbl.text = "\(curent)6"
}
break
case 7:
if curent != "0" && (curent as NSString).length > 1{
var aString: String? = curent
aString = (aString! as NSString).substring(to: (aString! as NSString).length - 1)
inputNumberLbl.text = aString
}else if curent != "0" && (curent as NSString).length > 0 {
inputNumberLbl.text = "0"
}
break
case 8:
if curent == "0" {
inputNumberLbl.text = "1"
}else{
inputNumberLbl.text = "\(curent)1"
}
break
case 9:
if curent == "0" {
inputNumberLbl.text = "2"
}else{
inputNumberLbl.text = "\(curent)2"
}
break
case 10:
if curent == "0" {
inputNumberLbl.text = "3"
}else{
inputNumberLbl.text = "\(curent)3"
}
break
case 11:
print("roal back")
break
case 12:
if curent.contains(".") == false {
inputNumberLbl.text = "\(curent)."
}
break
case 13:
if curent != "0" {
inputNumberLbl.text = "\(curent)0"
}
break
case 14:
if curent.contains("-") {
inputNumberLbl.text = curent.replacingOccurrences(of: "-", with: "")
print("dot seted")
}else{
inputNumberLbl.text = "-\(curent)"
}
break
case 15:
print("show setting")
break
default:
print(num + 1)
}
}
}
//MARK: - UIColletionViewDataSourece
private let identifier = "converterCollectionViewCell"
extension ConvertViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 16
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = padCollectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! ConverterCollectionViewCell
let index = indexPath.row
cell.keypadButton.indexpath = indexPath as NSIndexPath
cell.keypadButton.addTarget(self, action: #selector(keypadOnTamped(_:)), for: .touchUpInside)
let keypad = cell.keypadButton!
switch index {
case 0:
keypad.setTitle("7", for: .normal)
keypad.setBackgroundImage(UIImage(named: "bg_keypad"), for: .normal)
break
case 1:
keypad.setTitle("8", for: .normal)
keypad.setBackgroundImage(UIImage(named: "bg_keypad"), for: .normal)
break
case 2:
keypad.setTitle("9", for: .normal)
keypad.setBackgroundImage(UIImage(named: "bg_keypad"), for: .normal)
break
case 3:
keypad.setTitle("C", for: .normal)
keypad.setBackgroundImage(UIImage(named: "bg_keypad"), for: .normal)
break
case 4:
keypad.setTitle("4", for: .normal)
keypad.setBackgroundImage(UIImage(named: "bg_keypad"), for: .normal)
break
case 5:
keypad.setTitle("5", for: .normal)
keypad.setBackgroundImage(UIImage(named: "bg_keypad"), for: .normal)
break
case 6:
keypad.setTitle("6", for: .normal)
keypad.setBackgroundImage(UIImage(named: "bg_keypad"), for: .normal)
break
case 7:
keypad.setTitle(nil, for: .normal)
keypad.setBackgroundImage(UIImage(named: "btn_back"), for: .normal)
break
case 8:
keypad.setTitle("1", for: .normal)
keypad.setBackgroundImage(UIImage(named: "bg_keypad"), for: .normal)
break
case 9:
keypad.setTitle("2", for: .normal)
keypad.setBackgroundImage(UIImage(named: "bg_keypad"), for: .normal)
break
case 10:
keypad.setTitle("3", for: .normal)
keypad.setBackgroundImage(UIImage(named: "bg_keypad"), for: .normal)
break
case 11:
keypad.setTitle(nil, for: .normal)
keypad.setBackgroundImage(UIImage(named: "btn_swap"), for: .normal)
break
case 12:
keypad.setTitle(".", for: .normal)
keypad.setBackgroundImage(UIImage(named: "bg_keypad"), for: .normal)
break
case 13:
keypad.setTitle("0", for: .normal)
keypad.setBackgroundImage(UIImage(named: "bg_keypad"), for: .normal)
break
case 14:
keypad.setTitle("+/-", for: .normal)
keypad.setBackgroundImage(UIImage(named: "bg_keypad"), for: .normal)
break
case 15:
keypad.setTitle(nil, for: .normal)
keypad.setBackgroundImage(UIImage(named: "btn_setting"), for: .normal)
default:
print("some ting error")
}
return cell
}
}
extension ConvertViewController: SetButtonUnitDelegate{
func setUnit(numButton: Int,id: Int) {
let data = DBManager.realm.objects(UnitModel.self).filter("id == \(id)").first!
if numButton == 1 {
unit1Button.setTitle(data.symbol, for: .normal)
unitDsc1Lbl.text = data.unitName
}else if numButton == 2 {
unit2Button.setTitle(data.symbol, for: .normal)
unitDsc2Lbl.text = data.unitName
}else{
print("noting for todo")
}
}
}
并在第二个viewcontroller中
mport UIKit
var inButton = Int()
class SubCategoryTVC: UITableViewController {
let subCat = DBManager.realm.objects(SubCategoryModel.self).filter("catId == \(inCategory)").sorted(byKeyPath: "sortOrder", ascending: true)
weak var delegate : SetButtonUnitDelegate?
let vc = ConvertViewController()
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return subCat.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return subCat[section].haveUnit.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: SubCategoryCell = tableView.dequeueReusableCell(withIdentifier: "subCategoryCell", for: indexPath) as! SubCategoryCell
let unit = subCat[indexPath.section].haveUnit[indexPath.row]
cell.symbolLabel.text = SubCategoryTVC.getSymbol(unitId: unit.id)
cell.titleSymLabel.text = unit.unitName
cell.dscLabel.text = unit.unitName
return cell
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? HeaderCell ?? HeaderCell(reuseIdentifier: "header")
let catTitle = HomeCVController.getCategori[inCategory].catName
let titleOfCat = "cat \(catTitle)"
let catImage = titleOfCat.lowercased()
let imageOfCat = catImage.replacingOccurrences(of: " ", with: "_")
header.iconImage.image = UIImage(named: imageOfCat)
header.titleLabel.text = subCat[section].subCatName
header.section = section
return header
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 70.0
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let idUnit = subCat[indexPath.section].haveUnit[indexPath.row].id
self.delegate?.setUnit(numButton: 1, id: idUnit)
inUnit = idUnit
self.dismiss(animated: true, completion: nil)
showConverter()
}
func showConverter() {
let SVC = self.storyboard?.instantiateViewController(withIdentifier: "convertViewController") as! ConvertViewController
let navigation = UINavigationController(rootViewController: SVC)
SVC.title = "some"
self.present(navigation, animated: true, completion: nil)
}
static func getSymbol(unitId: Int)->String{
let inSub = DBManager.realm.objects(UnitModel.self).filter("id == \(unitId)").first!
var data: String = ""
if inSub.symbol != "" {
data = inSub.symbol
}else{
// data = inSub.unitName
let aString = inSub.unitName
do{
let regex = try NSRegularExpression(pattern: "%(.*)%", options: [])
let tmp = aString as NSString
var results = [String]()
regex.enumerateMatches(in: aString, options: [], range: NSMakeRange(0, aString.characters.count), using: {
result,flags,stop in
if let range = result?.rangeAt(1){
results.append(tmp.substring(with: range))
}
})
if results.count == 0 {
let first = aString.components(separatedBy: " ")
data = first.first!
}
}catch{
print("some ting error")
}
}
return data
}
}
答案 0 :(得分:1)
您可以使用UILabel
在UIViewController
被解雇后更改Delegate
文字。
class SomeClass: UIViewController, ChangeLabelDelegate {
override func viewDidLoad() {
super.viewDidLoad()
viewConfiguration()
getAllDropDowns()
}
func buttonClick() {
let secondViewController : SecondViewController ....
secondViewController.delegate = self
}
func changeLabel() {
// change Label Text here
}
}
protocol ChangeLabelDelegate {
func changeLabel()
}
class SecondViewController: UIViewController {
weak var delegate: ChangeLabelDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
func changeLabel() {
if (delegate) {
delegate.changeLabel
}
}
通过此示例代码,您可以更改UILabel
文字。