将Swift转换为目标C

问题描述 投票:-1回答:1

我很快就有了一段代码。请帮助我转换为目标C。

下面的代码显示了带有取消和提交按钮的UIAlertController。 UITextView作为子视图添加到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)
        }
    }
}
objective-c swift uitextview uialertcontroller
1个回答
1
投票

下面是与您的Swift代码等效的Objective-C代码。

请注意,textView对象声明到视图控制器的@interface中并以viewDidLoad()方法进行分配。

@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
© www.soinside.com 2019 - 2024. All rights reserved.