Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在UITextField中键入文本时更新UILabel_Objective C_Xcode_Uitextfield_Uilabel_Uitextfielddelegate - Fatal编程技术网

Objective c 如何在UITextField中键入文本时更新UILabel

Objective c 如何在UITextField中键入文本时更新UILabel,objective-c,xcode,uitextfield,uilabel,uitextfielddelegate,Objective C,Xcode,Uitextfield,Uilabel,Uitextfielddelegate,这是我在StackOverflow上的第一篇文章,也是Xcode和Objective-C的新手。 我读过关于代表的书,也尝试过其他人的代码。但是,我想看看我是否可以自己创建一些使用委托的东西,而无需复制其他人的代码 为了理解委托,我试图让UITextField委托在用户每次键入键盘上的键时更新标签 我创建了一个xib文件,并将一个TextField连接到我的.h文件中的UITextField*myTextField和标签UILabel*myLabel 我还将添加到ViewController类的

这是我在StackOverflow上的第一篇文章,也是Xcode和Objective-C的新手。 我读过关于代表的书,也尝试过其他人的代码。但是,我想看看我是否可以自己创建一些使用委托的东西,而无需复制其他人的代码

为了理解委托,我试图让UITextField委托在用户每次键入键盘上的键时更新标签

我创建了一个xib文件,并将一个TextField连接到我的.h文件中的UITextField*myTextField和标签UILabel*myLabel

我还将添加到ViewController类的.h文件中

但是,我不确定使用哪种委托方法,因为UITextFieldDelegate.h中列出的可选委托方法似乎只有在编辑完成后才能更新。另外,我不确定是否应该将AppDelegate方法放在与我的xib或AppDelegate.h文件关联的.h文件中


如果您能在这方面提供指导,我将不胜感激。

您可以在UITextField上添加UIViewController作为通知观察者,如下所示:

[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

- (void)textFieldDidChange:(UITextField *)textField {
}

因此,如果您有一个UITextField myTextField链接到您的nib IB TextField对象,一个UILabel myLabel链接到您的nib IB Label对象,并且您已经将您的TextField指向了您的文件所有者,那么请尝试此操作,看看是否有帮助

我只是很快地把它打出来,但我没有测试它,所以先检查它是否有错误

// ViewController.h
@interface ViewController : UIViewController <UITextFieldDelegate> {
    IBOutlet UILabel *myLabel;
    IBOutlet UITextField *myTextField;
}
@property (nonatomic, retain) UILabel *myLabel;
@property (nonatomic, retain) UITextField *myTextField;
@end


//  ViewController.m
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    myLabel.text = myTextField.text;
    return YES;   
}
//ViewController.h
@界面ViewController:UIViewController{
IBUILabel*myLabel;
IBOutlet UITextField*myTextField;
}
@属性(非原子,保留)UILabel*myLabel;
@属性(非原子,保留)UITextField*myTextField;
@结束
//ViewController.m
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串
{
myLabel.text=myTextField.text;
返回YES;
}

这是一个解决方法,而您应该使用
UITEmport
协议。是否有applescript objc版本?谢谢基本上你需要一个动作和与之对应的协议如果你想将值设置为一个实例变量,你可以在didEndEditing动作中完成,但你必须实现
code
textfielddendediting`
import UIKit

class ViewController: UIViewController , UITextFieldDelegate {
    // Create a label Outlet
    @IBOutlet weak var label: UILabel!
    // TextField Outlet
    @IBOutlet weak var textField: UITextField!
   // Select the textfield and in connection inspector drag a action from editing changed to the source code
    @IBAction func changed(_ sender: Any) {
       // Set text to label
        label.text = textField.text
        }
    override func viewDidLoad() {
        super.viewDidLoad()
        //Delegate to self
        textField.delegate = self
        textField.autocorrectionType = .no
    }
    // Implement the protocols
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        return true
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
       textField.resignFirstResponder()
        return true
    }}