iOS 之给苹果自带的纯数字键盘添加完成事件

壤驷向明
2023-12-01

.h文件中添加一个按钮的属性

{

    UIButton *doneInKeyboardButton;

}


在.m文件中添加以下代码就可实现纯数字代码添加完成事件

- (void)viewDidLoad

{

    [super viewDidLoad];

    //注册通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}


#pragma mark 键盘

- (void)handleKeyboardWillHide:(NSNotification *)notification

{

    if (doneInKeyboardButton.superview)

    {

        [doneInKeyboardButton removeFromSuperview];

    }

}

- (void)handleKeyboardDidShow:(NSNotification *)notification

{

    if (doneInKeyboardButton == nil)

    {

        doneInKeyboardButton = [UIButton buttonWithType:UIButtonTypeCustom];

        

        CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;

        if(screenHeight==568.0f){//爱疯5

            doneInKeyboardButton.frame = CGRectMake(0, 568 - 53, 106, 53);

        }else{//3.5

            doneInKeyboardButton.frame = CGRectMake(0, 480 - 53, 106, 53);

        }

        

        doneInKeyboardButton.adjustsImageWhenHighlighted = NO;

        //图片直接抠腾讯财付通里面的= =!

        [doneInKeyboardButton setImage:[UIImage imageNamed:@"btn_done_up@2x.png"] forState:UIControlStateNormal];

        [doneInKeyboardButton setImage:[UIImage imageNamed:@"btn_done_down@2x.png"] forState:UIControlStateHighlighted];

        [doneInKeyboardButton addTarget:self action:@selector(finishAction) forControlEvents:UIControlEventTouchUpInside];

    }

    

    

    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];

    

    if (doneInKeyboardButton.superview == nil)

    {

        [tempWindow addSubview:doneInKeyboardButton];    // 注意这里直接加到window

    }

    

}

//点击完成后的时间

-(void)finishAction

{

    NSLog(@"aa");

}

 类似资料: