2015-06-26 262 views
0

我想點擊按鈕時顯示鍵盤,但情況是鍵盤有附件視圖,這是文本框。總之,我想打開帶有文本框的配件視圖的鍵盤。這是正確的方式來做到這一點..?鍵盤沒有打開按鈕點擊

我搜索了很多,但這些解決方案都沒有幫助我實現這一目標。 請幫幫我。

這裏是我的代碼: ViewController.m

@interface ViewController() <UITextFieldDelegate> 
{ 
    UITextField *txtNote; 
} 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIView *accView = [self createAccessoryView]; 
    [txtNote setInputAccessoryView:accView]; 
} 

- (UIView *)createAccessoryView 
{ 
    UIView *accessoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)]; 
    [accessoryView setBackgroundColor:[UIColor lightGrayColor]]; 
    [accessoryView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin]; 

    CGFloat x = self.view.frame.size.width-65; 

    txtNote = [[UITextField alloc] initWithFrame:CGRectMake(0, 5, x-5, 35)]; 
    txtNote.delegate = self; 
    txtNote.font = [UIFont systemFontOfSize:IS_IPAD?20.0:14.0]; 
    [accessoryView addSubview:txtNote]; 

    UIButton *btnSave = [UIButton buttonWithType:UIButtonTypeCustom]; 
    btnSave.frame = CGRectMake(x, 5, 60, 35); 
    btnSave.layer.cornerRadius = 5.0f; 
    btnSave.clipsToBounds = TRUE; 
    btnSave.titleLabel.font = [UIFont systemFontOfSize:18.0f]; 
    [btnSave setTitle:@"Save" forState:UIControlStateNormal]; 
    [btnSave setBackgroundColor:[UIColor blackColor]]; 
    [btnSave setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
    [btnSave addTarget:self action:@selector(doneWithNumberPad:) forControlEvents:UIControlEventTouchUpInside]; 
    [btnSave setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin]; 
    [accessoryView addSubview:btnSave]; 

    return accessoryView; 
} 

- (IBAction)addNote:(UIButton *)button 
{ 
    // On this click, I want to show keyboard. 
    [txtNote becomeFirstResponder]; 
} 

- (IBAction)doneWithNumberPad:(id)sender 
{ 
    [txtNote resignFirstResponder]; 
} 

回答

0

希望這有助於you..I正在逐漸與鍵盤附件視圖按鈕點擊:

#import "ViewController.h" 

    @interface ViewController()<UITextFieldDelegate>{ 
     UITextField *txtNote; 
     UIView *accessoryView ; 
    } 
    @end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // call create accessoryview 
    [self createAccessoryView]; 
} 

- (UIView *)createAccessoryView 
    { 
    accessoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 100, 200, 45)]; 
    [accessoryView setBackgroundColor:[UIColor lightGrayColor]]; 
    accessoryView.userInteractionEnabled = YES; 

    //assign your dynamic frame values 
    txtNote = [[UITextField alloc] initWithFrame:CGRectMake(20, 5, 100, 35)]; 
    txtNote.delegate = self; 
    txtNote.backgroundColor = [UIColor greenColor]; 
    txtNote.userInteractionEnabled =YES; 
    txtNote.keyboardAppearance = UIKeyboardAppearanceDefault; 
    txtNote.font = [UIFont systemFontOfSize:IS_IPAD?20.0:14.0]; 
    [accessoryView addSubview:txtNote]; 

    UIButton *btnSave = [UIButton buttonWithType:UIButtonTypeCustom]; 
    btnSave.frame = CGRectMake(200, 5, 60, 35); 
    btnSave.layer.cornerRadius = 5.0f; 

    // btnSave.clipsToBounds = TRUE; 

    btnSave.titleLabel.font = [UIFont systemFontOfSize:18.0f]; 
    [btnSave setTitle:@"Save" forState:UIControlStateNormal]; 
    [btnSave setBackgroundColor:[UIColor blackColor]]; 
    [btnSave setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 

    [btnSave addTarget:self action:@selector(doneWithNumberPad:) forControlEvents:UIControlEventTouchUpInside]; 
    [btnSave setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin]; 
    [accessoryView addSubview:btnSave]; 

    return accessoryView; 
} 

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
    { 
     NSLog(@"begin"); 
     return YES; 
    } 

//Call the accessory view with keyboard with button action 
    -(IBAction)addBu:(id)sender{ 
    [txtNote becomeFirstResponder]; 
    [self.view addSubview:accessoryView]; 

    } 
+0

附件視圖不附加鍵盤 – VRAwesome

+0

我想設置鍵盤附件視圖 – VRAwesome

0

CGFloat x = self.view.frame.size.width-65; 

txtNote = [[UITextField alloc] initWithFrame:CGRectMake(0, 5, x-5, 35)]; 
txtNote.delegate = self; 
txtNote.font = [UIFont systemFontOfSize:IS_IPAD?20.0:14.0]; 
[accessoryView addSubview:txtNote]; 

應該是

CGFloat x = self.view.frame.size.width-65; 

txtNote = [[UITextField alloc] initWithFrame:CGRectMake(0, 5, x-5, 35)]; 
txtNote.delegate = self; 
txtNote.font = [UIFont systemFontOfSize:IS_IPAD?20.0:14.0]; 
[self.view addSubview:txtNote]; 
+0

你是對的。但我也希望在附件視圖中使用textField。 – VRAwesome

+0

textField的框架可以位於附件視圖中,但不能作爲附件視圖的子視圖。這是一個保留週期 – dopcn