激活UITextField时立即更改第一响应者

时间:2017-05-05 03:54:44

标签: ios objective-c swift uitextfield

我一直坚持这个问题。

我有一个inputAccessoryView我想用于键盘,而inputAccessoryView中有一个UITextField。这是应该激活键盘的文本字段,但不能,因为它存在于inputAccessoryView

我通过视图控制器UITextField向主机视图添加了第二个viewDidLoad。第二个UITextField被隐藏起来,只是通过becomeFirstResponder激活键盘的一种方法。

我为自定义inputAccessoryView设置了一个委托,当用户解除属于附件视图中存在的文本字段的键盘时,会调用该委托。我在第二个隐藏键盘上的代理函数中也有代码resignFirstResponder。这很好用,花花公子。

我目前遇到的问题是在becomingFirstResponder时遇到类似行为。我想这样做,当隐藏的键盘成为第一响应者时,它会立即调用附件视图中第二个键盘上的第一个响应者,使其看起来就像它刚刚被激活一样。

我不确定如何实现这一目标。我尝试在我的视图控制器上实现UITextField's委托并覆盖textFieldDidBeginEditing并在那里进行becomeFirstResponder调用,但无济于事。

关于为什么不起作用或采取其他方法的任何解释?

编辑:我已遵循建议here。它确实允许第一响应者所有权转移,但这样做有点不稳定(以前的键盘仍然显示一瞬间),现在以前工作得很好的resignFirstResponder进程根本不起作用。我想在textFieldDidBeginEditing来电期间resignFirstResponder被触发,并再次呼叫becomeFirstResponder。我现在无法解雇键盘。

1 个答案:

答案 0 :(得分:0)

在开始一个新的空项目并且稍微探讨了一下后,我提出了以下解决方案,到目前为止,它看起来效果很好并且符合预期。

 public void findBT() {

    try {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        if(mBluetoothAdapter == null) {
            Toast.makeText(getApplicationContext(),"Bluetooth Not Found...!",Toast.LENGTH_LONG).show();
        }

        if(!mBluetoothAdapter.isEnabled()) {
            Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBluetooth, 0);
        }

        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

        if(pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                Log.d("Devices","============>"+device.getName());
                // RPP300 is the name of the bluetooth  device
                // we got this name from the list of paired devices
                //if (device.getName()=="NP100S28C9") {
                mmDevice = device;
                break;

            }

        }


    }catch(Exception e){
        e.printStackTrace();
    }
}
// tries to open a connection to the bluetooth device
public void openBT() throws IOException {

   try {

        // Standard SerialPortService ID
        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

        if(mmDevice != null) {
            mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
            mmSocket.connect();
            mmOutputStream = mmSocket.getOutputStream();
            mmInputStream = mmSocket.getInputStream();

            beginListenForData();

             btnPrint.setEnabled(true);

        }
        else{

            Toast.makeText(getApplicationContext(),"Paired Bluthooth not Found..!",Toast.LENGTH_SHORT).show();
        }

    } catch (Exception e) {
        pDialog.dismiss();
        e.printStackTrace();

    }
}


public void beginListenForData() {
    try {

        final Handler handler = new Handler();
        // this is the ASCII code for a newline character
        final byte delimiter = 10;

        stopWorker = false;
        readBufferPosition = 0;
        readBuffer = new byte[1024];

        workerThread = new Thread(new Runnable() {
            public void run() {

                while (!Thread.currentThread().isInterrupted() && !stopWorker) {

                    try {

                        int bytesAvailable = mmInputStream.available();

                        if (bytesAvailable > 0) {

                            byte[] packetBytes = new byte[bytesAvailable];
                            mmInputStream.read(packetBytes);

                            for (int i = 0; i < bytesAvailable; i++) {

                                byte b = packetBytes[i];
                                if (b == delimiter) {

                                    byte[] encodedBytes = new byte[readBufferPosition];
                                    System.arraycopy(
                                            readBuffer, 0,
                                            encodedBytes, 0,
                                            encodedBytes.length
                                    );

                                    // specify US-ASCII encoding
                                    final String data = new String(encodedBytes, "US-ASCII");
                                    readBufferPosition = 0;

                                    // tell the user data were sent to bluetooth printer device
                                    handler.post(new Runnable() {
                                        public void run() {
                                            myLabel.setText(data);
                                        }
                                    });

                                } else {
                                    readBuffer[readBufferPosition++] = b;
                                }
                            }
                        }

                    } catch (IOException ex) {
                        stopWorker = true;
                    }

                }
            }
        });

        workerThread.start();

    } catch (Exception e) {
        e.printStackTrace();
    }
}