【发布时间】:2014-03-30 19:55:29
【问题描述】:
有没有办法让以下方法“全局”:
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//some code here
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
//some code here
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//some code here
}
我解释一下,我有一个带有 ARC 和 Storyboard 的 Objective-C 项目,我的每个视图都有自己的视图控制器类。我的项目中有很多文本字段,有时当用户开始编辑时,键盘会隐藏内容。所以我在网上搜索以找到解决此问题的方法。本主题的答案:Scrolling content hidden by keyboard. xcode 4.3.2 帮了我很多,它正在工作,它滚动内容,所以用户总是能够看到视图的底部。
但现在这是我的问题,我想让这个功能(开始编辑时滚动内容)在我的项目的每个地方都处于活动状态。未优化的解决方案是在我的每个视图类中复制代码。但我想知道是否有任何方法可以导入包含这些方法或其他内容的类。
我的实际代码和这里几乎一样(答案):Scrolling content hidden by keyboard. xcode 4.3.2
我希望某个地方有一些 Objective-C 大师可以帮助我解决问题。
-编辑-
我在 Storyboard 中的每个视图都有自己的 UIViewControllerClass。每个带有 TextField 的都有这个代码:
VCExample.h
@interface VCExample : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *oneTextField;
@end
VCExample.m
#import "VCExample.h"
#import "TextFieldScroll.h"
@interface VCExample ()
@end
@implementation VCExample
- (void)viewDidLoad
{
[super viewDidLoad];
TextFieldScroll *myTextFieldClass = [[TextFieldScroll alloc] init];
[self.usernameTextField setDelegate: myTextFieldClass];
self.usernameTextField.delegate = [TextFieldScroll shared];
}
@end
TextFieldScroll.h
#import <Foundation/Foundation.h>
@interface TextFieldScroll : NSObject <UITextFieldDelegate>
+ (id)shared;
@property CGFloat animatedDistance;
@end
TextFieldScroll.m
#import "TextFieldScroll.h"
@implementation TextFieldScroll
@synthesize animatedDistance;
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
+ (id)shared
{
static TextFieldScroll *sharedMyTextFieldHandler = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
{
sharedMyTextFieldHandler = [[self alloc] init];
});
return sharedMyTextFieldHandler;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect = [self.my.window convertRect:textField.bounds fromView:textField];
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
【问题讨论】:
标签: ios iphone objective-c storyboard textfield