输入时我正在格式化UITextField的文本。代码:
public void insertToDatabase(List<Account> listOfAccounts, Connection connection){
PreparedStatement preparedStatement = null;
try
{
preparedStatement = connection.prepareStatement( INSERT_ACCOUNT_QUERY );
}
catch( SQLException e1 )
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
for( Account account : listOfAccounts )
{
connection.setAutoCommit( false );
preparedStatement.setString( 1, account.getFirstName( ) );
preparedStatement.setString( 2, account.getLastName( ) );
preparedStatement.setInt( 3, account.getHomeTellNumber( ) );
preparedStatement.setInt( 4, account.getMobileTellNumber( ) );
preparedStatement.setString( 5, account.getAddress( ) );
preparedStatement.setString( 6, account.getCity( ) );
preparedStatement.setString( 7, account.getState( ) );
preparedStatement.setString( 8, account.getJob( ).getCode( ) );
preparedStatement.setString( 9, account.getLocale( ).getCode( ) );
preparedStatement.addBatch( );
}
preparedStatement.executeBatch( );
}
catch( SQLException e )
{
// TODO Auto-generated catch block
e.printStackTrace( );
}
finally
{
try
{
preparedStatement.close( );
}
catch( SQLException e )
{
// TODO Auto-generated catch block
e.printStackTrace( );
}
}
}
但是- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *newString = nil;
// format 'newString' here
[textField setText:newString];
// move cursor
return NO;
}
将在主线程中挂起一段时间。文字更少,时间更多。
因此,我尝试了以下代码,setText:
之前的resignFirstResponder
:
setText:
它解决了挂起问题。
但是为什么会挂起。还有什么更好的解决方案。
答案 0 :(得分:0)
newText
是什么?您是说newString
(您定义为nil
)。请注意,UITextField
的{{1}}不是text
。