ios - 关于 iOS 中 Popup Window 的 UITextView

标签 ios objective-c uiview uiviewcontroller popupwindow

我有UIViewController Manager

@interface Manager : UIViewController

@property   NSString *currentNameID;


@end

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


}

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

-(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;
    }

现在回调函数在弹出窗口中不起作用。 我把<UITextFieldDelegate>在经理处,如上所示。但它不起作用。

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

最佳答案

您的代码中有多个错误:

  1. 您正在使用 UITextView 并将协议(protocol)设置为 UITextFieldDelegate。要么制作 UITextViewDelegate 要么更改 UITextField

如果您使用UITextViewDelegate,则应使用以下委托(delegate)方法:

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

  1. 您正在将协议(protocol)设置为自类,因此需要更改代码行: self.idTextView.delegate = self;

关于ios - 关于 iOS 中 Popup Window 的 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32900599/

相关文章:

ios - 在 iPad 的横屏模式下,数字应该显示哪个键盘?

objective-c - 调出以符号为主的 iPad 键盘

ios - 在 if 和 case 语句中添加多个数字

ios - 如何在 objective-c 中为此代码创建函数

ios - 如何获取嵌套在 UIView 中的 UIView 框架 - Objective C

iphone - 如何确定平移手势的真实最终速度?

ios - PLCrashReporter - 符号问题

iphone - AFNetworking表单请求(在一个请求中上传多个文件)

ios - 使用NSDateFormatter将NSString解析为NSDate

ios - 在 Swift 中以编程方式在两个 subview 之间添加约束