UIView, UIScrollView и UITextFields вызов метода - PullRequest
0 голосов
/ 16 февраля 2010

У меня есть представление с несколькими встроенными UITextFields, этот UIView подчинен UIScrollView в IB. Предполагается, что каждое текстовое поле вызывает метод updateText, определенный в файле реализации viewcontroller, когда пользователь завершает редактирование поля. По какой-то причине метод updateText никогда не вызывается. У кого-нибудь есть идеи, как это исправить? Метод срабатывает очень хорошо, когда UIScrollView не присутствует в проекте, но клавиатура покрывает текстовые поля во время ввода, что раздражает. Теперь мои текстовые поля перемещаются над клавиатурой, когда она появляется, но не выключают метод после завершения редактирования.

Вот мой файл реализации:


#import "MileMarkerViewController.h"


@implementation MileMarkerViewController

@synthesize scrollView,milemarkerLogDate,milemarkerDesc,milemarkerOdobeg,milemarkerOdoend,milemarkerBusiness,milemarkerPersonal,milemarker;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Initialization code
    }
    return self;
}

- (BOOL) textFieldShouldReturn: (UITextField*) theTextField {
    return [theTextField resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver: self 
                                             selector: @selector(keyboardWasShown:)
                                                 name: UIKeyboardDidShowNotification 
                                               object: nil];

    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(keyboardWasHidden:)
                                                 name: UIKeyboardDidHideNotification 
                                               object: nil];

    keyboardShown = NO; // 1
    [scrollView setContentSize: CGSizeMake( 320, 480)]; // 2
}


- (void)keyboardWasShown:(NSNotification*)aNotification {
    if (keyboardShown) return;

    NSDictionary* info = [aNotification userInfo];

    // Get the size of the keyboard.
    NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;

    // Resize the scroll view (which is the root view of the window)
    CGRect viewFrame = [scrollView frame];
    viewFrame.size.height -= keyboardSize.height;
    scrollView.frame = viewFrame;

    // Scroll the active text field into view.
    CGRect textFieldRect = [activeField frame];
    [scrollView scrollRectToVisible:textFieldRect animated:YES];

    keyboardShown = YES;
}

- (void)keyboardWasHidden:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];

    // Get the size of the keyboard.
    NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;

    // Reset the height of the scroll view to its original value
    CGRect viewFrame = [scrollView frame];
    viewFrame.size.height += keyboardSize.height;
    [scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
    scrollView.frame = viewFrame;

    keyboardShown = NO;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    activeField = nil;
}




- (IBAction)updateText:(id) sender {

    NSLog(@"You just entered: %@",self.milemarkerLogDate.text);
    self.milemarker.logdate = self.milemarkerLogDate.text;
    self.milemarker.desc = self.milemarkerDesc.text;
    self.milemarker.odobeg = self.milemarkerOdobeg.text;
    self.milemarker.odoend = self.milemarkerOdoend.text;
    self.milemarker.business = self.milemarkerBusiness.text;
    self.milemarker.personal = self.milemarkerPersonal.text;
    NSLog(@"Original textfield is set to: %@",self.milemarker.logdate);
    [self.milemarker updateText];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
    [super dealloc];
}


@end

1 Ответ

0 голосов
/ 07 июня 2010

Я думаю, что вы уже знаете решение по дате публикации.

Вы можете реализовать метод textFieldShouldReturn и уволить первого респондента из textField в методе.

textFieldDidEndEditing вызывается после отставки textField первым респондентом.

...