如果/ Else TextView操作iOS

时间:2015-12-18 06:20:09

标签: ios objective-c

我的textview中有一个viewcontroller。文本将在textview上更改,但我希望能够做到的是。当textview显示某个单词时,会运行一些代码。所以我必须发表if/else声明:

这样的东西
textview == "Dance" {
RUN THIS CODE
}

但我会将textview的名称放在my textview而不是Option Explicit Sub CustomizedInputFixedoutput() Dim CopyRng As Range Dim PasteRng As Range Dim Msg As String Dim Response As VbMsgBoxResult Set CopyRng = Selection On Error Resume Next Set PasteRng = Application.InputBox("Select a cell to copy to", Type:=8) On Error GoTo 0 If Not PasteRng Is Nothing Then 'user clicked Cancel If PasteRng.Count > 1 Then 'Get confirmation to paste to multi-cell range Msg = "Are you sure you want to paste to " & PasteRng.Address & "?" _ & vbCrLf & vbCrLf _ & "Results may be unexpected if you proceed." Response = MsgBox(Msg, vbQuestion + vbYesNo, "Confirm multi-cell paste range") End If If Response = vbYes Or PasteRng.Count = 1 Then CopyRng.Copy PasteRng.Parent.Activate PasteRng.Activate ActiveSheet.Paste Link:=True Else MsgBox "Cancelled", vbInformation End If Else MsgBox "Cancelled", vbInformation End If Application.CutCopyMode = False End Sub

我应该如何为textview启动if / else语句?

6 个答案:

答案 0 :(得分:1)

添加文本字段委托方法

-(void)textViewDidBeginEditing:(UITextView *)textView 
{ 
   if ([<textview name>.text isEqualToString:@"Dance"])
   {
       // here add your code
    }
   else 
    {

    }
}

答案 1 :(得分:1)

您可以创建textView的IBOutlet(根据您更改的要求),以了解textView编辑是否是您要观察的。我们假设您将该地址命名为self.yourTargetTextView,那么这就是您想要的代表。

-(void)textViewDidBeginEditing:(UITextView *)textView 
{ 
   if(textView==self.yourTargetTextView)
   {
       NSString *string = textView.text;
       if ([string rangeOfString:@"dance"].location == NSNotFound) {
             NSLog(@"string does not contain dance, no code needs to run");
          } 
       else {
             NSLog(@"string contains dance!, lets run some code");
            }

    }
    else
    {
      //For other textViews
    }

}

或者您可以使用标签来区分不同的textViews。让我们假设您为目标textView分配标签,如:

self.yourTargetTextView.tag = 89267;//You can also do this through IB

然后你可以像这样编写你的委托方法:

-(void)textViewDidBeginEditing:(UITextView *)textView 
    { 
       if(textView.tag==89267)
       {
           NSString *string = textView.text;
           if ([string rangeOfString:@"dance"].location == NSNotFound) {
                 NSLog(@"string does not contain dance, no code needs to run");
              } 
           else {
                 NSLog(@"string contains dance!, lets run some code");
                }

        }
       else
       {
          //For other textViews
        }

}

答案 2 :(得分:0)

你必须实现UITextviewDelegate method.set textview对象委托给self和class应该确认为Protocol

- (void)textViewDidBeginEditing:(UITextView *)textView
{
  // if else condition

}

答案 3 :(得分:0)

提供UITextView标记。根据标签你可以把开关或if-else。在 textViewDidBeginEditing 上执行此操作。

-(void)textViewDidBeginEditing:(UITextView *)textView 
{ 
   if (textView.tag == 10)
   {
       // here add your code
    }
   else 
    {
    }
}

答案 4 :(得分:0)

尝试为所有文字视图设置唯一标记。假设您的Apple文本视图标记值为123。

-(void)textViewDidBeginEditing:(UITextView *)textView 
{ 
   if(textView.tag == 123){
     if ([textView.text isEqualToString:@"Dance"])
   {
       // true condition code goes here.
    }
   else 
    {
      // else part goes here if necessary
    }
   }
}

答案 5 :(得分:0)

尝试下面的textView Delegate方法:

当textView文本发生变化时,将调用此方法,您可以在其中查看所需内容。

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"something"]) {
        NSLog(@"do whatever you like here");
        return NO; // or YES, whatever you like
    }
    else{

        return YES; // or NO, whatever you like
    }

    return YES;
}