2016-09-01 38 views
0

我想知道是否有可能使一个UIButton输入文本到文本框中按下迅速制作UIButtons文本输入到一个文本框

override func viewDidLoad() { 
super.viewDidLoad() 

var textBox = UITextField(frame: CGRectMake(x, y, w, h)) 
    textBox.borderStyle = UITextBorderStyle.Line 
    self.view.addSubview(textBox) 

var button = UIButton(frame: CGRectMake(x, y, w, h)) 
    button.backgroundColor = UIColor.blueColor() 
    self.view.addSubview(button) 
} 

时有没有一种方法,使VAR按钮输入文本到文本框(V​​AR的textBox)

+0

你为什么要这样一个按钮,你的按钮?什么是用法? –

+0

制作计算器UI – void00

+0

@Peter你会知道一种可能的方式吗? – void00

回答

0

在您选择的方法添加到使用addTarget

var textBox = UITextField! 
var button = UIButton! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    textBox = UITextField(frame: CGRectMake(x, y, w, h)) 
    textBox.borderStyle = UITextBorderStyle.Line 
    self.view.addSubview(textBox) 
    button = UIButton(frame: CGRectMake(x, y, w, h)) 
    button.backgroundColor = UIColor.blueColor() 
    button.addTarget(self, 
     action:#selector(buttonTapped), 
     forControlEvents: UIControlEvents.TouchUpInside) 
    self.view.addSubview(button) 
} 

func buttonTapped(object:AnyObject) { 
    textBox?.text = "your text" 
} 
+0

功能buttonTapped无法检测到viewdidload中的文本框 – void00

+0

@ Ice3343对于输入错误,使用iPad进行响应以及自动更正会弄乱代码。 buttonTapped方法应该在viewdidload之外var button:UIButton!也应该在viewdidload之外。 –

+0

是的你的答案奏效 – void00

相关问题