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

时间:2015-10-06 07:17:31

标签: ios objective-c iphone uibutton

我的iPhone有iOS8。我想在UIKeyboardTypeNumberPad上添加DONE按钮。 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];
}

2 个答案:

答案 0 :(得分:0)

试一试。这种方法适用于iOS 6,7&amp; 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)

答案 1 :(得分:0)

sorry for bad english

我之前复制了你的代码来检查,错误是什么,经过一些改变它的工作正常但是Frame 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
}

它对我的工作,希望它也适合你。