Search code examples
objective-cswiftuitextviewuialertcontroller

Convert Swift to Objective C


I have a piece of code in swift. Please help me convert to Objective C.

The below code shows a UIAlertController with a cancel and submit button. UITextView is added as subview to alertController.

let textView = UITextView(frame: CGRect.zero)

@IBAction func sendFeedback(_ sender: Any) {
    let alertController = UIAlertController(title: "Feedback \n\n\n\n\n", message: nil, preferredStyle: .alert)

    let cancelAction = UIAlertAction.init(title: "Cancel", style: .default) { (action) in
        alertController.view.removeObserver(self, forKeyPath: "bounds")
    }
    alertController.addAction(cancelAction)

    let saveAction = UIAlertAction(title: "Submit", style: .default) { (action) in
        let enteredText = self.textView.text
        alertController.view.removeObserver(self, forKeyPath: "bounds")
    }

    alertController.addAction(saveAction)

    alertController.view.addObserver(self, forKeyPath: "bounds", options: NSKeyValueObservingOptions.new, context: nil)
    textView.backgroundColor = UIColor.white
    textView.textContainerInset = UIEdgeInsets.init(top: 8, left: 5, bottom: 8, right: 5)
    alertController.view.addSubview(self.textView)

    self.present(alertController, animated: true, completion: nil)
}


override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "bounds"{
        if let rect = (change?[NSKeyValueChangeKey.newKey] as? NSValue)?.cgRectValue {
            let margin: CGFloat = 8
            let xPos = rect.origin.x + margin
            let yPos = rect.origin.y + 54
            let width = rect.width - 2 * margin
            let height: CGFloat = 90

            textView.frame = CGRect.init(x: xPos, y: yPos, width: width, height: height)
        }
    }
}

Solution

  • The equivalent Objective-C code for your Swift code is written below.

    Note that the textView object declare into the @interface of the view controller and allocate in viewDidLoad() method.

    @interface ViewController ()
    
    @property (nonatomic, strong)   UITextView *textView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.textView = [[UITextView alloc] initWithFrame:CGRectZero];
    }
    
    
    - (IBAction) sendFeedback:(id)sender {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Feedback \n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alertController.view removeObserver:self forKeyPath:@"bounds"];
        }];
        [alertController addAction:cancelAction];
    
        UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *enteredText = self.textView.text;
            [alertController.view removeObserver:self forKeyPath:@"bounds"];
        }];
        [alertController addAction:saveAction];
    
        [alertController.view addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionNew context:nil];
        self.textView.backgroundColor = [UIColor whiteColor];
        self.textView.textContainerInset = UIEdgeInsetsMake(8, 5, 8, 5);
        [alertController.view addSubview:self.textView];
    
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
        if ([keyPath isEqualToString:@"bounds"]) {
            if (change[NSKeyValueChangeNewKey]) {
                NSValue *value = change[NSKeyValueChangeNewKey];
                CGRect rect = value.CGRectValue;
                CGFloat margin = 8;
                CGFloat xPos = rect.origin.x + margin;
                CGFloat yPos = rect.origin.y + 54;
                CGFloat width = rect.size.width - 2 * margin;
                CGFloat height = 90;
    
                self.textView.frame = CGRectMake(xPos, yPos, width, height);
            }
        }
    }
    
    @end