๐Ÿ“ฑ App Development Study/iOS๐ŸŽ

[Udemy iOS & Swift Bootcamp] UITextField, UITextFieldDelegate

ibelieveinme 2023. 4. 25. 22:28
728x90

* UITextField ์ƒ์„ฑ/๊ธฐ๋ณธ ์†์„ฑ

- UITextField ์—๋„ Dark/LIght Mode๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ ์šฉ๋œ๋‹ค.

- Text Input Type์„ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

- Return Key ๋Š” ํ‚ค๋ณด๋“œ์—์„œ ํ™•์ธ?๋ฒ„ํŠผ์„ Enter๋‚˜ Go ๋“ฑ ์–ด๋–ป๊ฒŒ ๋ณด์ด๊ฒŒ ํ• ์ง€ ์„ค์ •ํ•˜๋Š” ๊ฑฐ๋‹ค.  Default๋Š” Return.

- Sezure Text Entry๋Š” ์•”ํ˜ธํ•„๋“œ์—์„œ ์ž…๋ ฅ๋œ ๊ธ€์ž๊ฐ€ ์•ˆ๋ณด์ด๊ฒŒ ํ•œ๋‹ค.

 

* ์‚ฌ์šฉ

ViewController ์Šคํฌ๋ฆฝํŠธ์— UITextField ์—ฐ๊ฒฐํ•ด์„œ IBOutlet, IBAction ๋งŒ๋“ค์–ด์ฃผ๊ธฐ

ํ‚ค๋ณด๋“œ ์•ˆ๋‚˜์˜ค๋ฉด Command + k ๋ˆ„๋ฅด๋ฉด Keyboard ํ† ๊ธ€๋ผ์„œ ์˜ฌ๋ผ์˜จ๋‹ค.

 

* Return Key ๋ˆŒ๋ €์„ ๋•Œ ๋ฐ˜์‘ํ•˜๊ฒŒ ํ•˜๊ธฐ

1. UITextFieldDelegate ํด๋ž˜์Šค ์ถ”๊ฐ€ํ•˜๊ณ  UITextField ์— Delegate ์ ์šฉํ•˜๊ธฐ

UITextField์˜ delegate๋กœ ViewController๋ฅผ ๋ถ€์—ฌํ–ˆ๋‹ค๋Š” ๊ฒƒ์€ ์‚ฌ์šฉ์ž๊ฐ€ UITextField ์ž‘์—…์„ ํ•  ๋•Œ๋งˆ๋‹ค UITextField๊ฐ€ ViewController์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

ex) UITextField ์™ˆ: "ViewController! ์œ ์ €๊ฐ€ ํƒ€์ดํ•‘์„ ์„ ํƒํ–ˆ์–ด."

 

2. ์‹คํ–‰

searchPressed ํ•จ์ˆ˜์™€ textFieldShouldReturn ํ•จ์ˆ˜ ์•ˆ์— searchTextField.endEditing(true) ๊ตฌ๋ฌธ์„ ์ ์–ด์ฃผ๋ฉด ํ‚ค๋ณด๋“œ๊ฐ€ ์ž๋™์œผ๋กœ ๋‚ด๋ ค๊ฐ„๋‹ค.

 

* ๊ธฐํƒ€ UITextField Delegate ํ•จ์ˆ˜๋“ค ์ฐธ๊ณ 

 

UITextFieldDelegate | Apple Developer Documentation

A set of optional methods to manage the editing and validation of text in a text field object.

developer.apple.com

 

728x90