UIView、UIScrollView 和 UITextFields 问题调用方法

Posted

技术标签:

【中文标题】UIView、UIScrollView 和 UITextFields 问题调用方法【英文标题】:UIView, UIScrollView and UITextFields problem calling Method 【发布时间】:2010-02-16 19:04:45 【问题描述】:

我有一个带有几个嵌入式 UITextFields 的视图,这个 UIView 从属于 IB 中的 UIScrollView。当用户完成编辑字段时,每个文本字段都应该调用 viewcontroller 实现文件中定义的名为 updateText 的方法。出于某种原因,方法 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

【问题讨论】:

我会说再次验证操作并确保 xib 已保存...至少这是我在使用 IB 时一直遇到的问题。 【参考方案1】:

由于发布日期,我认为您已经知道解决方案。

您可以实现textFieldShouldReturn 方法并从方法中的textField 中退出第一响应者。

textFieldDidEndEditing 在 textField 退出第一响应者后被调用。

【讨论】:

以上是关于UIView、UIScrollView 和 UITextFields 问题调用方法的主要内容,如果未能解决你的问题,请参考以下文章

UIScrollView 和 UIView 的约束

在 UIView (XIB) 和滚动视图之上的 UIScrollView 不会滚动

UIView、UIScrollView 和 UITextFields 问题调用方法

UIScrollView 上的 UIView 菜单

UIScrollView - 无法查看所有 UIView

不使用 UIScrollview 缩放 UIView