如何在用户在UITextField中编辑其详细信息时显示保存按钮? (斯威夫特3)

时间:2017-09-18 09:42:40

标签: ios swift xcode swift3 uitextfield

我希望我的保存按钮仅在用户编辑其用户名/电子邮件时显示。我将保存按钮的alpha设置为0,因此如果用户编辑了某些内容,则按钮的alpha将为1,这是他唯一可以保存其详细信息的时间。出于某种原因,我不能让它像我想要的那样工作。

这是我的代码:

import UIKit
import Parse

class SettingsTableViewController: UITableViewController, UITextFieldDelegate {

@IBOutlet var currentUsername: UITextField!
@IBOutlet var currentEmail: UITextField!
@IBOutlet var saveButton: UIButton!


let currentUser = PFUser.current()

override func viewDidLoad() {
    super.viewDidLoad()

    currentUsername.text = currentUser?.username
    currentEmail.text = currentUser?.email

}


func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    saveButton.alpha = 1
    return true
}



@IBAction func saveButtonPressed(_ sender: Any) {

    if currentUsername.text != currentUser?.username {

        currentUser?.saveInBackground()

    }

}


@IBAction func signoutButtonPressed(_ sender: Any) {

    PFUser.logOut()

    performSegue(withIdentifier: "userSignedOut", sender: self)

}

}

提前致谢!

Here's a screenshot for reference

2 个答案:

答案 0 :(得分:1)

请试试这个

private int preLast;
// Initialization stuff.
yourListView.setOnScrollListener(this);

// ... ... ...

@Override
public void onScroll(AbsListView lw, final int firstVisibleItem,
        final int visibleItemCount, final int totalItemCount)
{

    switch(lw.getId()) 
    {
        case R.id.your_list_id:     

            // Make your calculation stuff here. You have all your
            // needed info from the parameters of this function.

            // Sample calculation to determine if the last 
            // item is fully visible.
            final int lastItem = firstVisibleItem + visibleItemCount;

            if(lastItem == totalItemCount)
            {
                if(preLast!=lastItem)
                {
                    //to avoid multiple calls for last item
                    Log.d("Last", "Last");
                    preLast = lastItem;
                }
            }
    }
}

答案 1 :(得分:1)

编辑更改时,可以使用文本字段添加目标。 以下是您可以在课堂上使用的代码。

override func viewDidLoad() {
    super.viewDidLoad()
    currentUsername.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)
    currentEmail.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)
}

func textFieldDidChange(_ textField: UITextField) {
    saveButton.alpha = currentUsername.text?.characters.count != 0 && currentEmail.text?.characters.count != 0 ? 1 : 0
}

我希望你隐藏/取消隐藏按钮而不是更改alpha

func textFieldDidChange(_ textField: UITextField) {
    saveButton.isHidden = currentUsername.text?.characters.count == 0 || currentEmail.text?.characters.count == 0
}