繁体   English   中英

为什么不调用UIButton的选择器方法?

[英]Why UIButton's selector method is not called?

我的iPhone有iOS8。 我想在UIKeyboardTypeNumberPad上添加DONE按钮。 完成显示在左侧的空白按钮上。 但是当我触摸(按)DONE时,不会调用选择器方法doneButtonClicked 我的代码如下。 代码有什么问题?

- (void)viewWillAppear:(BOOL)animtated {

    // Register the observer for the keyboardWillShow event
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];

}

- (void)viewWillDisappear:(BOOL)animtated {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    // create custom button

    if (!self.doneButton)
    {
        self.doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.doneButton addTarget:self action:@selector(doneButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

    }

    self.doneButton.adjustsImageWhenHighlighted = NO;
    [self.doneButton setTitle:@"DONE" forState:UIControlStateNormal];
    [self.doneButton.titleLabel setFont:[UIFont systemFontOfSize:16.0]];
    [self.doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];

    // locate keyboard view
    if ([[[UIApplication sharedApplication] windows] count] <= 1) return;
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++)
    {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard found, add the button
        if ([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
        {
            BOOL isPortrait = UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
            self.doneButton.frame = CGRectMake(((isPortrait)?0:-1),((int) (keyboard.frame.size.height*3)/4) + ((isPortrait)?0:1),(int) keyboard.frame.size.width/3-1, (isPortrait)?60:40);
            [keyboard addSubview:self.doneButton];
        }
        //This code will work on iOS 8.0
        else if([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES)
        {
            for(int i = 0 ; i < [keyboard.subviews count] ; i++)
            {
                UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i];
                if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES)
                {
                    BOOL isPortrait = UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
                    self.doneButton.frame = CGRectMake(((isPortrait) ? 0 : -1),((int) (hostkeyboard.frame.size.height*3)/4) + ((isPortrait) ? 0 : 1),(int) hostkeyboard.frame.size.width/3-1, (isPortrait) ? 60 : 40);
                    [hostkeyboard addSubview:self.doneButton];
                }
            }
        }
        else{}
    }
}

- (void)doneButtonClicked:(id)sender{
    [self.heightUpdateTextField resignFirstResponder];
}

试试看 此方法适用于iOS 6、7和8:

- (void)keyboardWillShow:(NSNotification *)iNotification
    // If there's no keyboard yet, don't do anything
    if ([[[UIApplication sharedApplication] windows] count] < 2) {
        return;
    }

    UIWindow *keyboardWindow = [[UIApplication sharedApplication] windows][1];

    // Create new dismiss keyboard button
    UIButton *aDismissKeyboardButton = [UIButton buttonWithType:UIButtonTypeCustom];

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.3")) {
        aDismissKeyboardButton.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 53, [[UIScreen mainScreen] bounds].size.width / 3, 53);
    } else {
        aDismissKeyboardButton.frame = CGRectMake(0, 163, 106, 53);
    }

    aDismissKeyboardButton.adjustsImageWhenHighlighted = FALSE;
    [aDismissKeyboardButton setExclusiveTouch:YES];

    UIImage *aDismissKeyboardButtonImageNormal;
    UIImage *aDismissKeyboardButtonImageHighlighted;

    aDismissKeyboardButtonImageNormal = [UIImage imageNamed:@"myImage.png"];
    aDismissKeyboardButtonImageHighlighted = [UIImage imageNamed:@"myImageHighLighted.png"];

    [aDismissKeyboardButton setImage:aDismissKeyboardButtonImageNormal forState:UIControlStateNormal];
    [aDismissKeyboardButton setImage:aDismissKeyboardButtonImageHighlighted forState:UIControlStateHighlighted];
    SEL aDoneButtonSelector = NSSelectorFromString(@"doneButtonPressed");
    [aDismissKeyboardButton addTarget:iDelegate action:aDoneButtonSelector forControlEvents:UIControlEventTouchUpInside];


    // Locate keyboard view and add button
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.3")) {
        for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
            if ([[window description] hasPrefix:@"<UITextEffectsWindow"]) {
                [window addSubview:aDismissKeyboardButton];
                return;
            }
        }
    } else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        for (UIView *aSubview in [[keyboardWindow.subviews objectAtIndex:0] subviews]) {
            if ([[aSubview description] hasPrefix:@"<UIInputSetHost"]) {
                [aSubview addSubview:aDismissKeyboardButton];
                return;
            }
        }
    } else {
        NSString *keyboardPrefix = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0") ? @"<UIPeripheralHost" : @"<UIKeyboard";

        for (UIView *aSubView in keyboardWindow.subviews) {
            if ([[aSubView description] hasPrefix:keyboardPrefix]) {
                [aSubView addSubview:aDismissKeyboardButton];
                return;
            }
        }
    }
}

在这里,我定义了一个宏来检查系统版本,如下所示:

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

sorry for bad english

稍早更改完我的代码后,我就复制了您的代码以检查它是什么错误,但是“ Frame of done ButtonFrame of done Button有所不同,您可以根据自己的意愿进行更改,请检查已编辑的代码。

- (void)viewWillAppear:(BOOL)animtated {

    // Register the observer for the keyboardWillShow event
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];

}

- (void)viewWillDisappear:(BOOL)animtated {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    // create custom button


     UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [doneButton addTarget:self action:@selector(doneButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
     doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setTitle:@"DONE" forState:UIControlStateNormal];
    [doneButton.titleLabel setFont:[UIFont systemFontOfSize:16.0]];
    [doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];

    // locate keyboard view
    if ([[[UIApplication sharedApplication] windows] count] <= 1) return;
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++)
    {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard found, add the button
        if ([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
        {
            BOOL isPortrait = UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
            doneButton.frame = CGRectMake(((isPortrait)?0:-1),((int) (keyboard.frame.size.height*3)/4) + ((isPortrait)?0:1),(int) keyboard.frame.size.width/3-1, (isPortrait)?60:40);
            [keyboard addSubview:doneButton];
        }
        //This code will work on iOS 8.0
        else if([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES)
        {
            for(int i = 0 ; i < [keyboard.subviews count] ; i++)
            {
                UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i];
                if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES)
                {
                    BOOL isPortrait = UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
                    doneButton.frame = CGRectMake(((isPortrait) ? 0 : -1),((int) (hostkeyboard.frame.size.height*3)/4) + ((isPortrait) ? 0 : 1),(int) hostkeyboard.frame.size.width/3-1, (isPortrait) ? 60 : 40);
                    [hostkeyboard addSubview:doneButton];
                }
            }
        }
        else{}
    }
}

- (void)doneButtonClicked:(id)sender{
    [self->cityTxt resignFirstResponder]; //cityTxt is my textfield
}

它对我有用,希望它对您也有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM