2013-12-12 127 views
0

我想要一個沒有任何文本框的數字鍵盤,我想按下一個按鈕並彈出鍵盤,有沒有簡單的方法來做到這一點? 或者我需要建立自己的鍵盤?如何在沒有UITextField的情況下彈出鍵盤

謝謝。 :)

+0

這可能有所幫助:http://stackoverflow.com/questions/4384072/how-to-show-a-keyboard-via-press-a-nsbutton – user523234

+0

使用[UIKeyInput](https://developer.apple的.com /庫/ IOS /文檔/ UIKit的/參照/ UIKeyInput_Protocol /參考/的reference.html) –

回答

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 

此代碼可以幫助你......

相關問題