关于 Window 中的 Popup iOS 的 UITextView

Regarding the Popup Window's UITextView in iOS

我有UIViewController Manager

@interface Manager : UIViewController

@property   NSString *currentNameID;


@end

@interface Manager ()<UITextFieldDelegate>{
    sqlite3 *RECORDS;
    sqlite3_stmt *statement;
    NSString *databasePath;
    NSString *docsDir;
    NSArray *dirPaths;


}

在管理器 UIViewController 中,我想创建一个弹出窗口 Window。 它被创建为

-(void)createPopUpWindow
{
    //Component specs
    int gap = 20;//to update
    int bigButtonWidth = 70;//to update
    int componentverHeight = 40;//to update

    int numcomponentsvertically = 4;
    int numcomponentshorizontally = 3;
    int textViewW = bigButtonWidth*2;
    int smallButtonWidth = 20;
    int uiviewWidth = gap * 4 + bigButtonWidth * numcomponentshorizontally;
    int uiviewHeight = gap * 5 + componentverHeight * numcomponentsvertically;
    // create view popup
    int centerx = (int)(self.view.bounds.size.width / 2);
    int centery = (int)(self.view.bounds.size.height / 2);

    //Popup window
    int x = centerx - (int)(uiviewWidth/2);
    int y = centery - (int)(uiviewHeight/2);
    self.viewPopup = [[UIView alloc] initWithFrame:CGRectMake(x, y, uiviewWidth, uiviewHeight)];
    self.viewPopup.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.viewPopup];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];

    [self.viewPopup addGestureRecognizer:tap];



    self.idTextView = [[UITextView alloc] initWithFrame:CGRectMake(gap, gap, textViewW, componentverHeight)];
    [self.idTextView setReturnKeyType:UIReturnKeyDone];
    self.idTextView.restorationIdentifier = @"idview";
    self.idTextView.delegate = self.viewPopup;
    [self.viewPopup addSubview: self.idTextView];

}

-(void)dismissKeyboard {
    if([self.currTextfield isEqualToString:@"idview"]){
        [self.idTextView resignFirstResponder];
    }
}

然后我还需要UITextView的回调函数来获取一些关于UITextView的信息比如

    - (BOOL) textFieldShouldBeginEditing: (UITextField *) textField
    {
        textField.text = @"";
        self.currTextfield = textField.restorationIdentifier;
    }

现在回调函数在 Popup 中不起作用 Window。 如上所示,我将 <UITextFieldDelegate> 放在 Manager 中。但是没用。

如何让它工作以便调用 UITextView 的回调? 然后在这一行中,应该分配给谁来委托 self.idTextView.delegate = self.viewPopup; 谢谢

您的代码中有多个错误:

  1. 您正在使用 UITextView 并将协议设置为 UITextFieldDelegate。制作 UITextViewDelegate 或更改 UITextField.

如果您正在使用 UITextViewDelegate,您应该使用以下委托方法:

- textViewShouldBeginEditing: - textViewDidBeginEditing: - textViewShouldEndEditing: - textViewDidEndEditing:

  1. 您正在将协议设置为 self class 因此需要更改代码行: self.idTextView.delegate = self;