2011-07-15 18 views
26

說,清一色我有一個的UITextField設置爲安全,但希望給用戶,使之不固定選項(所以他們可以確定他們輸入的內容是否在私人區域)。但是,假設他們錯誤地觸發了切換,並且想要將其更改回安全模式?這是行不通的。我試過所有的東西 - 用-1代替YES,刪除文本然後放回去。我在其他想法上完全失敗。 [輸入rdar:// 9781908]的UITextField secureTextEntry - 作品是從去NO,但變回是沒有效果的上述

編輯:我相信這個問題是固定的iOS5。

+0

給我在這裏的一些代碼從UISwitch –

+0

//潤色,設置狀態應該是什麼 //工作在ON-> OFF,但改變它OFF-> ON沒有任何效果, /開關入住在非安全模式從此 - (IBAction爲)secureSwitchAction:(UISwitch *)發件人 { \t BOOL ISON = sender.on; \t textField.secureTextEntry = isOn; } –

+1

感謝並保重。:) –

回答

46

它必須輸入焦點問題:焦點時的UITextField只能更改ON-> OFF。
嘗試下招切換OFF-> ON:

textField.enabled = NO; 
textField.secureTextEntry = YES; 
textField.enabled = YES; 
[textField becomeFirstResponder]; 

EDIT(dhoerl):在iOS系統9.3,我發現這個工作,但有一個問題。如果在普通視圖中輸入3個字符,然後切換到安全文本,然後鍵入一個字符,則3個預先存在的字符將消失。我嘗試了各種技巧來清除,然後重置文本沒有成功。我終於嘗試通過剪切和粘貼來控制控制 - 粘貼到新切換到安全模式下運行得非常好。所以我用代碼模擬了它,現在一切正常 - 無需與響應者一起玩。以下是我終於結束了:

if textview should go into secure mode and the textfield is not empty { 
     textField.text = "" 
     textField.secureTextEntry = true 

     UIPasteboard.generalPasteboard().string = password 
     textField.paste(self) 
     UIPasteboard.generalPasteboard().string = "" 
    } 

我寧願沒有這樣做糊,但如果你想它無縫對接,這是我能找到這樣做的唯一途徑。

+1

是的 - 我確實發現,只要控件不是第一響應者,就可以按預期工作。一隻小鳥告訴我,這可能會在未來的iOS版本中解決。 –

+0

因此,我在我的修復程序的liu上面測試了您的代碼 - textField顯然會在第一個響應者失效時辭職。也就是說,追加[textField becomeFirstResponder];導致工作解決方案比我的簡單得多。謝謝! –

+0

僅供參考我試過這個解決方案,並在secureTextEntry被設置爲NO後導致額外的空間:http://stackoverflow.com/questions/14220187/uitextfield-has-trailing-whitespace-after-securetextentry-toggle –

1

我對這個問題進入了rdar,但確實找到了解決辦法。基本上你必須以編程方式替換新的「卡住」控件。最容易做的事情是在存檔的viewDidLoad中現有的控制解除封存,然後根據需要:

// do in viewDidLoad 
self.passwordMemberArchive = [NSMutableData data]; 
NSKeyedArchiver *ka = [[NSKeyedArchiver alloc]  initForWritingWithMutableData:passwordMemberArchive]; 
[ka encodeObject:password]; 
[ka finishEncoding]; 
[ka release]; 


    // In the action method when you get the UISwitch action message --- 
// when your switch changes state 
if(isOn) { 
    NSString *text = [NSString stringWithString:password.text]; 
    NSKeyedUnarchiver *kua = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; 
    UITextField *tf = [kua decodeObject]; 
    [kua finishDecoding]; 
    [kua release]; 
    tf.inputAccessoryView = textField.inputAccessoryView; 
    tf.frame = textField.frame; 

    BOOL isFirstResponder = [textField isFirstResponder]; 
    [scrollView insertSubview:tf aboveSubview:textField]; 
    if(isFirstResponder) { 
     [tf becomeFirstResponder]; 
    } 

    [textField removeFromSuperview]; 
    self.password = tf; 

    if([text length]) { 
     if(isFirstResponder) { 
      // http://stackoverflow.com/questions/1317929/insert-string-at-cursor-position-of-uitextfield 
      // Get a reference to the system pasteboard 
      UIPasteboard* lPasteBoard = [UIPasteboard generalPasteboard]; 
      // Save the current pasteboard contents so we can restore them later 
      NSArray* lPasteBoardItems = [lPasteBoard.items copy]; 
      // Update the system pasteboard with my string 
      lPasteBoard.string = text; 
      // Paste the pasteboard contents at current cursor location 
      [tf paste:self]; 
      // Restore original pasteboard contents 
      lPasteBoard.items = lPasteBoardItems; 
      [lPasteBoardItems release]; 
     } else { 
      tf.text = text; 
     } 
    } 
} else { 
    textField.secureTextEntry = NO; 
} 
+0

請注意MikeR提供的更好的解決方案: –

22

邁克的r解決方案是不錯,但我更喜歡這種方法:只becomeFirstResponder再次在必要時

BOOL wasFirstResponder; 
if ((wasFirstResponder = [passwordField isFirstResponder])) { 
    [passwordField resignFirstResponder]; 
} 
// In this example, there is a "show password" toggle 
[passwordField setSecureTextEntry:![passwordField isSecureTextEntry]]; 
if (wasFirstResponder) { 
    [passwordField becomeFirstResponder]; 
} 

這樣你。

+0

對我來說,這似乎是一個更好的解決方案,謝謝桑迪。我已經將它封裝在我的應用程序的一個小函數中。 – JosephH

+0

我剛剛提出了這個答案,因爲當我嘗試第一個答案時,在將secureTextEntry設置爲NO時,我在文本末尾有一個神祕的尾部空格。這個解決方案沒有這個問題。 –

+0

+1完美!正在讓我瘋狂! – Groot

0

有了iOS,如果你想要的行爲不是由框架提供的,你應該永遠不要試圖「破解」東西,改變主意!

首先它更容易^^,第二用戶將不會響應這一點,那麼你永遠不會知道下一個iOS的更新將打破它或沒有,所以它可能是危險的應用程序。

「你想讓用戶看到他在一個安全的文本框錄音,密碼爲」,可以顯示在底部,而不是一個UILabel?或者使用清除密碼的確認警報框?

+0

有趣的是,該功能已在這是一款受歡迎的應用程序,在商店裏評價爲5星,評價爲2年,並且對此的評價是肯定的。請注意,改變選項的能力已經存在,蘋果甚至在報告後修復了這個問題 –

+0

是的,功能在這裏,當然這個功能是一個好主意,我從來沒有說過!我只是說你應該考慮使用乾淨的方式(如我的意思是iOS代碼)使你的功能活着! –

0

桑迪解決方案的快速版本。

if #available(iOS 9.2, *) { 
    passwordTextField.secureTextEntry = !passwordTextField.secureTextEntry 
} 
else { 
    let wasFirstResponder = passwordTextField.isFirstResponder() 
    if wasFirstResponder { 
     passwordTextField.resignFirstResponder() 
    } 

    passwordTextField.secureTextEntry = !passwordTextField.secureTextEntry 
    if wasFirstResponder { 
     passwordTextField.becomeFirstResponder() 
    } 
} 
相關問題