【问题标题】:UIButton disappears when my application returns from running in the background当我的应用程序从后台运行返回时 UIButton 消失
【发布时间】:2012-03-03 16:56:03
【问题描述】:

我有一个小项目,其中包含一些带有数字键盘的 UITextField。当显示键盘时,我正在添加一个按钮作为子视图,供用户关闭键盘。

但是,如果键盘处于活动状态并且我关闭了应用程序,我添加的按钮将在重新启动应用程序时消失。 (应用程序通过多任务处理保持非活动状态,因此不会完全退出。)

这是我用来添加按钮的代码(我在 xib 中配置的“完成”按钮)。

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(keyboardWillShow)name:UIKeyboardWillShowNotification object:nil];

    [super viewDidLoad];
}

- (void)keyboardWillShow{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    // We must perform on delay (schedules on next run loop pass) for the keyboard subviews to be present.
    [self performSelector:@selector(addHideKeyboardButtonToKeyboard) withObject:nil afterDelay:0]; 
}

- (void)addHideKeyboardButtonToKeyboard{
    // Locate non-UIWindow.
    doneButton.hidden=NO;

    UIWindow *keyboardWindow = nil;
    for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
        if (![[testWindow class] isEqual:[UIWindow class]]) {
            keyboardWindow = testWindow;
            break;
        }
    }
    if (!keyboardWindow) return;

    // Locate UIKeyboard.  
    UIView *foundKeyboard = nil;
    for (UIView __strong *possibleKeyboard in [keyboardWindow subviews]) {

        // iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView.
        if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
            possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0];
        }                                                                                

        if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) {
            foundKeyboard = possibleKeyboard;
            break;
        }
    }

    if (foundKeyboard) {
        [doneButton setImage:[UIImage imageNamed:@"doneupHeb.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"donedownHeb.png"] forState:UIControlStateHighlighted];
        doneButton.frame = CGRectMake(-1, 163, 106, 53);
        [foundKeyboard addSubview:doneButton];
        // Add the button to foundKeyboard.
    }

}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    [loan resignFirstResponder];
    [YearCycle resignFirstResponder];
    [prime resignFirstResponder];
    [MothlyReturn resignFirstResponder];   

    [doneButton removeFromSuperview]; 
    doneButton = nil;
}


- (void)textFieldDidBeginEditing:(UITextField *)textField{

    textField.delegate=self;
    //editingField = textField;

    if ([prime isFirstResponder]||[MothlyReturn isFirstResponder]){

        scroll.contentOffset = CGPointMake(0, 166 );
    }
 //   if ([YearCycle isFirstResponder]){
       // scroll.contentOffset = CGPointMake(0, 200);

}

- (IBAction)closeNumpad:(id)sender{

    [loan resignFirstResponder];
    [YearCycle resignFirstResponder];
    [prime resignFirstResponder];
    [MothlyReturn resignFirstResponder];

    scroll.contentOffset = CGPointMake(0, 0);
    doneButton.hidden=YES;
}

【问题讨论】:

    标签: objective-c iphone uikeyboard


    【解决方案1】:

    我在网站上其他问题的帮助下解决了这个问题 - 对于所有有或将有问题的人 - 这是代码: 请注意:按钮本身是在xib文件中设计的,而不是在代码中。

    .h 文件:

    BOOL firstTime;
    BOOL add;
    BOOL keyboardOpened;
    IBOutlet UIButton *doneButton;
    

    .m 文件:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        firstTime = TRUE;
        add = TRUE;
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)viewDidUnload
    {
    
        //  [[NSNotificationCenter defaultCenter] removeObserver:self];
    
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    
    - (void)addButtonToKeyboard {
        // create custom button
        /* UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
         doneButton.frame = CGRectMake(0, 163, 106, 53);
         doneButton.adjustsImageWhenHighlighted = NO;
         [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
         [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
         doneButton.tag = 3;
         [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];*/
        // locate keyboard view
        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:@"<UIPeripheralHostView"] == YES && add){
                doneButton.frame = CGRectMake(-1, 163, 106, 53);
                [keyboard addSubview:doneButton];
    
            }    
        }
    }
    
    - (void)removeButtonFromKeyboard
    {
        // locate keyboard view
        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, remove the button
            if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) [[keyboard viewWithTag:3] removeFromSuperview];
        }
    }
    
    - (IBAction)doneButton:(id)sender {
        [loan resignFirstResponder];
        [YearCycle resignFirstResponder];
        [ageOfCeo resignFirstResponder];
        [YearofBusiness resignFirstResponder];
        scroll.contentOffset = CGPointMake(0, 0);
    
        if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"])
        {
            [self removeButtonFromKeyboard];
            firstTime = TRUE;
        }
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
        [theTextField resignFirstResponder];
        return YES;
    }
    
    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        if ([ageOfCeo isFirstResponder]||[YearofBusiness isFirstResponder]){
    
            scroll.contentOffset = CGPointMake(0, 166 );
        }
    
        //   firstResponder = textField;
    }
    
    - (void)keyboardDidShow:(id)sender
    {
        if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"]) 
        {
            NSLog(@"%@",[[UIDevice currentDevice] model]);
            [self addButtonToKeyboard];
            keyboardOpened = TRUE;
        }
    }
    
    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
    
        if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"])
        {
            [self removeButtonFromKeyboard];
            keyboardOpened = FALSE;
        }
    }
    

    【讨论】:

    • 如果它是 IBOutlet,您不必担心指定什么时候在 xib 或情节提要中指定。
    【解决方案2】:

    我认为您的问题是因为您正在删除 - (void)keyboardWillShow 中的观察者 .. 尝试将这一行 [[NSNotificationCenter defaultCenter] removeObserver:self]; 放在 -(void)viewDidUnload

    【讨论】:

      猜你喜欢
      • 2011-05-22
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      相关资源
      最近更新 更多