HJH IT Logs

objc2C++UIAlertController

naver.how 2021. 5. 31. 13:30
반응형

- (IBAction)onClickAmount:(id)sender {

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];

    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

        textField.placeholder = @"물량을 입력하세요";

        textField.secureTextEntry = NO;

        textField.keyboardType = UIKeyboardTypeNumberPad;

    }];

    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"물량을 입력하세요 %@", [[alertController textFields][0] text]);

        //compare the current password and do action here

 

    }];

    [alertController addAction:confirmAction];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"Canelled");

    }];

    [alertController addAction:cancelAction];

    [self presentViewController:alertController animated:YES completion:nil];

}

C++에는 iOS와 같은 네이티브 UI 프레임워크가 없으므로, iOS의 UIAlertController와 같은 기능을 C++에서 직접 구현하는 것은 어렵습니다. 대신, Qt 라이브러리와 같은 크로스 플랫폼 C++ GUI 프레임워크를 사용하여 유사한 기능을 구현할 수 있습니다. 아래는 Qt를 사용해서 비슷한 기능을 수행하는 코드 예제입니다.

먼저, Qt가 설치되어 있는지 확인해야 하며, 설치되어 있지 않다면 Qt 공식 웹사이트에서 설치할 수 있습니다. 설치 후, Qt Creator를 사용하여 새로운 Qt Widget Application 프로젝트를 생성할 수 있습니다.

```cpp
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QInputDialog>
#include <QMessageBox>
#include <QDebug>

class MyWidget : public QWidget {
    Q_OBJECT

public:
    MyWidget(QWidget *parent = nullptr) : QWidget(parent) {
        QPushButton *button = new QPushButton("Click Amount", this);
        button->setGeometry(50, 50, 200, 50);
        connect(button, &QPushButton::clicked, this, &MyWidget::onClickAmount);
    }

private slots:
    void onClickAmount() {
        bool ok;
        QString text = QInputDialog::getText(this, tr("Input Amount"),
                                             tr("물량을 입력하세요:"), QLineEdit::Normal,
                                             "", &ok);
        if (ok && !text.isEmpty()) {
            qDebug() << "물량을 입력하세요" << text;
            // compare the current input and do action here
        } else {
            qDebug() << "Cancelled";
        }
    }
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    MyWidget window;
    window.resize(320, 240);
    window.setWindowTitle("Qt Example");
    window.show();

    return app.exec();
}

#include "main.moc"
```

이 예제에서는 `QInputDialog`를 사용하여 텍스트 입력을 받을 수 있도록 하고, `QMessageBox`를 사용하여 메시지를 출력할 수 있도록 합니다. `qDebug()`를 사용하여 콘솔에 로그를 출력할 수 있습니다.

Qt 프로젝트를 설정하는 방법과 사용법에 대해 더 알고 싶다면 Qt 공식 문서를 참조하면 많은 도움이 됩니다. 이 방법을 통해 iOS의 UIAlertController와 유사한 기능을 C++에서 구현할 수 있습니다.

반응형