关于iOS中的Popup Window的UITextView

时间:2015-10-02 04:33:51

标签: 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>放在Manager上,如上所示。但它没有用。

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

1 个答案:

答案 0 :(得分:1)

您的代码中存在多个错误:

  1. 您正在使用UITextView,并且您将协议设置为UITextFieldDelegate。要么UITextViewDelegate要么改变UITextField
  2. 如果您使用UITextViewDelegate,则应使用以下委托方法:

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

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