xcode 如何检测我的 UITextView 何时被编辑,然后调用一个动作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7914980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:15:02  来源:igfitidea点击:

How can I detect when my UITextView is being edited and then call an action?

iphoneobjective-ciosxcodeuitextview

提问by pixelbitlabs

When my UITextView is being edited, I want a button to appear. How can I do this?

当我的 UITextView 被编辑时,我想要一个按钮出现。我怎样才能做到这一点?

Thanks!

谢谢!

回答by aahsanali

Implemetn UITextViewDelegatein your class and when editing is being started on UITextViewit'll call it's textViewShouldBeginEditing:function where you can do whatever you want. You can show your button when editing start and hides when editing ends in this function.

ImplemetnUITextViewDelegate在你的类以及正在就开始编辑UITextView,它会调用它的textViewShouldBeginEditing:功能,你可以做任何你想要的。在此功能中,您可以在编辑开始时显示您的按钮,并在编辑结束时隐藏您的按钮。

– textViewShouldEndEditing

For further detail see this

有关更多详细信息,请参阅此

回答by helloiloveit

i will detail above answer a little bit:

我将详细说明上面的答案:

YourViewController.h

你的视图控制器.h

@interface YourViewController : UIViewController <UITextViewDelegate>

YourViewController.m

YourViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.yourTextView.delegate = self;

}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
   // do smth like create a button 
   return TRUE;
}

Read Apple manualfor more

阅读苹果手册了解更多

回答by serge-k

Xcode 6.4, iOS 8.4, ARC Enabled

Xcode 6.4, iOS 8.4, ARC 已启用

Don't make my mistake, took me a long time to figure it out, but I was adding...

不要犯我的错误,我花了很长时间才弄明白,但我正在补充......

@interface YourViewController : UIViewController <UITextFieldDelegate>

because I am used to working with UITextField's, but obviously we need...

因为我习惯于使用 UITextField 的,但显然我们需要......

@interface YourViewController : UIViewController <UITextViewDelegate>

Hope this saves someone time. Cheers!

希望这可以节省某人的时间。干杯!

回答by odrm

Check out the UITextViewDelegate Protocol Reference. If you set your UIViewController to be the delegateof the UITextView, it will be sent textViewDidBeginEditing:and textViewDidEndEditing:. You can show/hide your button when you receive those messages.

查看UITextViewDelegate 协议参考。如果您将 UIViewController 设置delegate为 UITextView 的 ,它将被发送textViewDidBeginEditing:textViewDidEndEditing:。当您收到这些消息时,您可以显示/隐藏您的按钮。

回答by Kattu Poochi

//in view did load.

//在视图中加载。

UITextField entered1 = [[UITextField alloc] initWithFrame:CGRectMake(120.0, 85.0, 150.0, 25.0)]; 
[entered1 setBackgroundColor:[UIColor whiteColor]]; 
[entered1 addTarget:self action:@selector(example:)forControlEvents:UIControlEventTouchDown];
[self.view addSubview:entered1];
[entered1 release];  

or

或者

  [textfieldname addTarget:self action:@selector(example:)forControlEvents:UIControlEventTouchDown];
        .........
  }
  -(IBAction) example:(UIControl *)sender
  {
   //your code here.
  }