[英]update and get text from uitextfield inside uicollectionciewcell
[英]How to get the text of a UItextfield to update?
我有一個uitextfield,我嘗試使用從Internet查詢的信息進行更新,但是,直到我單擊該文本字段,該文本才會顯示在該文本字段中。 如何使用目標c自行顯示文本?
我正在使用以下方式將文本放在文本字段中
self.textfield.text = title;
其中title是一個NSString。
if (error == nil) {
NSDictionary *responseBody = [NSJSONSerialization JSONObjectWithData: data options: 0 error: nil];
NSLog(@"got response: %@", responseBody);
NSString *title=[responseBody objectForKey:@"name"];
self.titleOfBook.text = title;
}
我不想使用占位符,如果網站上沒有存儲標題,我不希望它包含任何文本,並且網站上存儲了標題,我希望視圖控制器進行更新並顯示找到標題。 該代碼正確地找到了標題並將其分配給NSString *title
,但是直到我單擊文本字段時,文本字段才顯示標題。
嘗試這個
if (error == nil) {
NSDictionary *responseBody = [NSJSONSerialization JSONObjectWithData: data options: 0 error: nil];
NSLog(@"got response: %@", responseBody);
NSString *title=[responseBody objectForKey:@"name"];
self.titleOfBook.text = title;
}
else {
self.titleOfBook.text = @"";
}
否則,如果您不想顯示或出現占位符文本,則可以隱藏文本字段。
self.titleOfBook.hidden != title.length > 0; //It'll display only length greater than zero.
盡管如此,它不會更新,然后在主線程上可用。 UI更新將在主線程上工作。
dispatch_async(dispatch_get_main_queue(), ^{
self.titleOfBook.text = YOUR_OPERATION;
});
謝謝。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if( textField.text.length + string.length - range.length > 3) // lets say 3 chars mininum
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
text
NSDictionary *responseBody = [NSJSONSerialization JSONObjectWithData: data options: 0 error: nil];
NSLog(@"got response: %@", responseBody);
NSString *title=[responseBody objectForKey:@"name"];
self.titleOfBook.text = title;
});
});
}
return YES;
}
嘗試使用此功能。使用阻止功能可以幫助您...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.