如何在没有UITextField的情况下弹出键盘

时间:2013-12-12 11:44:24

标签: ios keyboard

我想要一个没有任何文本字段的数字键盘,我想按一个按钮,键盘会弹出,有没有简单的方法呢? 或者我需要建立自己的键盘?

感谢。 :)

2 个答案:

答案 0 :(得分:0)

//
//  EditingView.h
//  TextEditing
//
//  Created by Jeffrey Sambells on 10-04-21.
//  Copyright 2010 TropicalPixels. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface UIKeyInputExampleView : UIView <UIKeyInput> {
    NSMutableString *textStore;
}

@property (nonatomic, retain) NSMutableString *textStore;

@end


//
//  EditingView.m
//  TextEditing
//
//  Created by Jeffrey Sambells on 10-04-21.
//  Copyright 2010 TropicalPixels. All rights reserved.
//

#import "UIKeyInputExampleView.h"

@implementation UIKeyInputExampleView

@synthesize textStore;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
        self.textStore = [NSMutableString string];
        [self.textStore appendString:@"Touch screen to edit."];

        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

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

#pragma mark -
#pragma mark Respond to touch and become first responder.

- (BOOL)canBecomeFirstResponder { return YES; }

-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
    [self becomeFirstResponder];
} 

#pragma mark -
#pragma mark Drawing

- (void)drawRect:(CGRect)rect {
    CGRect rectForText = CGRectInset(rect, 20.0, 20.0);
    UIRectFrame(rect);
    [self.textStore drawInRect:rectForText withFont:[UIFont fontWithName:@"Helvetica" size:24.0f]];
}

#pragma mark -
#pragma mark UIKeyInput Protocol Methods

- (BOOL)hasText {
    if (textStore.length > 0) {
        return YES;
    }
    return NO;
}

- (void)insertText:(NSString *)theText {
    [self.textStore appendString:theText];
    [self setNeedsDisplay];
}

- (void)deleteBackward {
    NSRange theRange = NSMakeRange(self.textStore.length-1, 1);
    [self.textStore deleteCharactersInRange:theRange];
    [self setNeedsDisplay];
}

@end

此代码可以帮助您......

答案 1 :(得分:0)

您需要实现UIKeyInput协议。 Reference

相关问题