将 Swift 转换为 Objective C
Convert Swift to Objective C
我有一段代码在swift。请帮我 转换为 Objective 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)
}
}
}
您的 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
我有一段代码在swift。请帮我 转换为 Objective 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)
}
}
}
您的 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