문제

By clicking on the back button, I save the information from the UITextFields of the UIView. When the information in a UITextField is not correct and I click on the back button, I want to open the keyboard, but my view dissapears. How to stop the UIView from dissapearing and open my keyboard instead.

I think I should put my code in a viewShouldDissapear, but there is no such method.

This is my code:

- (void) viewWillDisappear:(BOOL)animated
{

if ([textField1 length] < 3 || [textField1 length] > 17) {

    tableView.contentInset = UIEdgeInsetsMake(65, 0, 10, 0);

    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:7 inSection:indexPath.section]
                     atScrollPosition:UITableViewScrollPositionTop animated:NO];

    [txtSerieSasiu becomeFirstResponder];
    return;
}

if (shouldSave)
    [self save];
}
도움이 되었습니까?

해결책

I'm assuming that you have back button on navigation controller.
Then add a custom button there and if everything is correct then dismiss it other wise show keyboard

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"goback.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(popVC:) forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake(0, 0, 32, 32)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)popVC:(id)sender
{
    if (isValueCorrect) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else{
        [textView becomeFirstResponder];
    }
}

다른 팁

Write this code in selector that is called from the button press and if everything is right then only pop/dismiss your viewController. As in viewWillDisAppear you can'l ask controller to cancel disappearing.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top