移动位于键盘初学者指南下的内容

时间:2014-03-19 05:33:09

标签: ios uiscrollview keyboard scrollview identifier

我根据应用程序构建了一个项目,这个项目是我在Apple"今天开始开发iOS应用程序"文档。我创建了一个基本的视图控制器类,带有一个文本框和两个按钮。但是,当我开始键入键盘覆盖我的内容时。

我在互联网上搜索了一个清晰简洁的解释,以实现那里提供的Apple示例代码"移动位于键盘下面的内容"那里的开发指南部分:


清单5-1处理键盘通知

// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(keyboardWasShown:)
        name:UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
         selector:@selector(keyboardWillBeHidden:)
         name:UIKeyboardWillHideNotification object:nil];

}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;

// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
    [self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}

我将此代码导入到类的实现块中后立即收到了几个错误:

  1. 使用未声明的标识符' activeField'

  2. 未知的类型名称' scrollView&#39 ;;你的意思是' UIScrollView'?

  3. 属性' scrollView'找不到类型对象' SignInViewController *'

  4. 经过一些研究并回到基础编程后,我在头文件中声明了标识符:

    UIScrollView *scrollView;
    // The UIScrollView class provides support for displaying content that is larger than the size of the application’s window. It enables users to scroll within that content by making swiping gestures, and to zoom in and back from portions of the content by making pinching gestures.
    
    UITextField *activeField;
    // A UITextField object is a control that displays editable text and sends an action message to a target object when the user presses the return button. You typically use this class to gather small amounts of text from the user and perform some immediate action, such as a search operation, based on that text.
    

    此外,我宣布了该物业" scollView"对于#34; SignInViewController"类型的对象在类的接口块中:

    @property(nonatomic, readonly, retain) UIScrollView *scrollView;
    

    清除了所有编译错误。 不幸的是,在编译和构建此代码之后,位于键盘下方的内容没有移动。 在编写此代码时,我做错了什么?如果可能的话,请你简单地用例子来解释它,因为我正在尝试使用面向对象的C语言。

    我包括下面的完成代码。谢谢你的帮助!:

    标题文件:SignInViewController.h

    #import <UIKit/UIKit.h>
    
    @interface SignInViewController : UIViewController
    
    @end
    
    UIScrollView *scrollView;
    // The UIScrollView class provides support for displaying content that is larger than the size of the application’s window. It enables users to scroll within that content by making swiping gestures, and to zoom in and back from portions of the content by making pinching gestures.
    
    UITextField *activeField;
    // A UITextField object is a control that displays editable text and sends an action message to a target object when the user presses the return button. You typically use this class to gather small amounts of text from the user and perform some immediate action, such as a search operation, based on that text.
    

    主文件:SignInViewController.m

    #import "SignInViewController.h"
    
    @interface SignInViewController ()
    
    //Declared property scrollView
    @property(nonatomic, readonly, retain) UIScrollView *scrollView;
    
    @end
    
    @implementation SignInViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    // Code to dismiss the keyboard
    - (IBAction)dismissKeyboard:(id)sender
    {
        [activeField resignFirstResponder];
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    //Moving Content That Is Located Under the Keyboard
    // Call this method somewhere in your view controller setup code.
    - (void)registerForKeyboardNotifications
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasShown:)
                                                     name:UIKeyboardDidShowNotification object:nil];
    
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillBeHidden:)
                                                     name:UIKeyboardWillHideNotification object:nil];
    
    }
    
    // Called when the UIKeyboardDidShowNotification is sent.
    - (void)keyboardWasShown:(NSNotification*)aNotification
    {
        NSDictionary* info = [aNotification userInfo];
    
        // Get the size of the keyboard
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
        // Adjust the bottom content inset of your scroll view by the keyboard height.
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
        scrollView.contentInset = contentInsets;
        scrollView.scrollIndicatorInsets = contentInsets;
    
        // If active text field is hidden by keyboard, scroll it so it's visible
        // Your app might not need or want this behavior.
        // Scroll the target text field into view.
        // Check if the active text field is under the keyboard. If it isn’t under the keyboard, the scrollview won’t update the content offset. If the active text field is under the keyboard we update the scroll view’s content offset.
        CGRect aRect = self.view.frame;
        aRect.size.height -= kbSize.height;
        if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
            [self.scrollView scrollRectToVisible:activeField.frame animated:YES];
        }
    }
    
    // Called when the UIKeyboardWillHideNotification is sent
    - (void)keyboardWillBeHidden:(NSNotification*)aNotification
    {
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        scrollView.contentInset = contentInsets;
        scrollView.scrollIndicatorInsets = contentInsets;
    }
    
    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        activeField = textField;
    }
    
    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
        activeField = nil;
    }
    @end
    

0 个答案:

没有答案