好吧,我对@sweepez建议的代码所做的更改似乎更合乎逻辑,除非现在我模拟应用程序时,如果有意义,我甚至无法打开第一个文本字段来开始输入。有没有办法为第一个或根据用户点击的内容设置活动的TextField?
if (textField == quarterNum){
doneButton.tag = 25
} else if (textField == dimeNum) {
doneButton.tag = 10
} else if (textField == nickelNum) {
doneButton.tag = 5
} else {
doneButton.tag = 1
}
return false
}
func doneBttnPressed (barButton: UIBarButtonItem) {
if (barButton.tag == 25) {
dimeNum.becomeFirstResponder()
} else if (barButton.tag == 10) {
nickelNum.becomeFirstResponder()
} else if (barButton.tag == 5) {
pennyNum.becomeFirstResponder()
}
}
这是我将所有内容移动到viewDidLoad功能时遇到的内容。我没有任何东西可以与文本字段进行比较,以便在它们上设置标记。
override func viewDidLoad() {
super.viewDidLoad()
quarterNum.delegate = self
dimeNum.delegate = self
nickelNum.delegate = self
pennyNum.delegate = self
////Background image///////
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "money1.png")!)
//Making A toolbar
let keyboardDoneButtonShow = UIToolbar(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/17))
//Setting the style for the toolbar
keyboardDoneButtonShow.barStyle = UIBarStyle.Black
//Making the done button and calling the textFieldShouldReturn native method for hidding the keyboard.
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: "doneBttnPressed:")
let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let toolbarButton = [flexSpace,doneButton]
keyboardDoneButtonShow.setItems(toolbarButton, animated: false)
quarterNum.inputAccessoryView = keyboardDoneButtonShow
dimeNum.inputAccessoryView = keyboardDoneButtonShow
nickelNum.inputAccessoryView = keyboardDoneButtonShow
pennyNum.inputAccessoryView = keyboardDoneButtonShow
if (textField == quarterNum){
doneButton.tag = 25
} else if (textField == dimeNum) {
doneButton.tag = 10
} else if (textField == nickelNum) {
doneButton.tag = 5
} else {
doneButton.tag = 1
}
}
答案 0 :(得分:1)
编辑:此外,最好在viewDidLoad上创建工具栏/按钮,并在开始编辑文本字段后简单地更改其标记。
根据您所在的文本字段创建“完成”按钮,例如quarterNum字段,然后为其设置标记,即25,或者如果dimeNum字段然后说标记10 ... 还必须更改动作功能。
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
// NOTE: Do all the stuff you are already doing to create the toolbar and buttons
// except the action function for the done button has to be a different method
// one of your own.
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: "doneBttnPressed:")
// ...
// Then set a tag to the done button based on the textfield that created it.
if (textField == quarterNum) {
doneButton.tag = 25
} else if (textField == dimeNum) {
doneButton.tag = 10
} etc etc
}
// Choose which textField to become active once 'Done' is pressed
func doneBttnPressed (barButton: UIBarButtonItem) {
if barButton.tag == 25 {
dimeNum.becomeFirstResponder()
} else if barButton.tag == 10 {
nickelNum.becomeFirstResponder()
} etc etc
}
答案 1 :(得分:0)
当您使用自定义完成按钮时,您是否向该按钮添加了触摸事件?您应该在触摸事件方法中执行某些操作。
类似这样的事情
[button addTarget:self selector:@selector(buttonAction:) controlEvent:UIControlEventTouchUpInside];
答案 2 :(得分:0)
在你的堆栈跟踪中我读了
ChangeCalculator [9944:300380] - [UIBarButtonItem resignFirstResponder]:无法识别的选择器发送到实例0x7f80dbd0a440 2016-03-19 01:07:00.405 ChangeCalculator [9944:300380] ***由于未捕获的异常'NSInvalidArgumentException'终止应用,原因:' - [UIBarButtonItem resignFirstResponder]:无法识别的选择器发送到实例0x7f80dbd0a440'
这告诉我发生了三件事之一。
选项1
你有一个ChangeCalculator
对象无法扩展NSObject
,因此无法响应选择器,这是一个错误,它已经好几次了。< / p>
选项2
在UIBarButtonItem
已经失去第一响应者状态后,您正在让Selector
辞职第一响应者。
选项3
您错误地编写了一个选择器。我忍不住注意到你在上述答案的评论中发表了什么。
在Swift中,选择器的编写没有您正在应用的Sub demo()
With ActiveDocument
Dim StrOut As String, DocNew As Document
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Format = False
.MatchWildcards = True
.Wrap = wdFindStop
.Text = "\/ - * \("
.Replacement.Text = ""
.Execute
End With
Do While .Find.Found
StrOut = StrOut & Replace(Replace(.Text, "/ - ", ""), " (", "") & vbCr
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End With
'Do whatever yo want to do with strOut.
'I don't have excel experience.
'I generally save it as text and chnge the ext from .txt to .csv which will open ok with excel.
Set DocNew = Documents.Add
With DocNew
.Range.Text = StrOut
.SaveAs2 FileName:="C:\Users\" & Environ("Username") & "\Desktop\i.txt", _
FileFormat:=wdFormatText, AddToRecentFiles:=False
.Close
End With
Set DocNew = Nothing
End Sub
包装。你应该只使用一个字符串文字。