Cherr Skees
Cherr Skees

Reputation: 1518

Updating a label as a user is typing

It seems like I need to use

@interface ViewController : UIViewController <UITextFieldDelegate>

and

- (void)textViewDidChange:(UITextView *)textView {
label.text = textField.text;    
}

and maybe something like this in viewDidLoad

- (void)viewDidLoad
{
[super viewDidLoad];


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

It crashes on entering text if I do it like this... Otherwise it just updates when its completed.

Upvotes: 1

Views: 834

Answers (2)

Nikita Pestrov
Nikita Pestrov

Reputation: 5966

You don't need last line. Use textField.delegate=self; instead.

Upvotes: 3

Christian Schnorr
Christian Schnorr

Reputation: 10786

Replace

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

with

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

since your method takes a parameter: the text field itself. This argument is part of the selector and represented by the colon.

Or just implement the UITextView delegate methods: there's a value-changed-callback, too.

Upvotes: 1

Related Questions