2011-02-23 62 views
2

我在UIActionsheet中创建textField,但我不能输入文本,任何人都可以有想法吗?如何在UIActionSheet中的UITextField中启用文本输入?

我的编码是遵循

-(void)commentsMethod: (id) sender { 

    //setup UITextField for the UIActionSheet 
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 28, 320, 150)]; 
    //textField.delegate=self; 
    textField.autocorrectionType = UITextAutocorrectionTypeNo; 
    [textField setBackgroundColor:[UIColor clearColor]]; 
    [textField setBorderStyle:UITextBorderStyleRoundedRect]; 
    [textField becomeFirstResponder]; 

    //CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>) 

    UIButton *btn = [[UIButton alloc] init]; 
    [btn setFrame:CGRectMake(0, 0, 100, 100)]; 
    [btn setTitle:@"dismiss" forState:UIControlStateNormal]; 
    [btn setBackgroundColor:[UIColor clearColor]]; 
    [btn addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside]; 



    UIActionSheet *myActionSheet; 
    myActionSheet = [[UIActionSheet alloc] initWithTitle:@"Comments" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil]; 
    myActionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque; 
    [myActionSheet showInView:self.view]; 
    myActionSheet.userInteractionEnabled=YES; 

    [myActionSheet setFrame:CGRectMake(0, 20, 320,600)]; 
    [myActionSheet insertSubview:textField atIndex:0]; 
    [myActionSheet insertSubview:btn atIndex:1]; 
    //[self.myActionSheet dismissWithClickedButtonIndex:0 animated:NO]; 

    //memory management 
    [textField release];  
    [myActionSheet release];   
} 
+0

为什么你无法输入文字? – Vladimir 2011-02-23 10:12:34

+0

无论我在键盘上点击,例如A B C D ,,,,在textField中什么也看不到, – 2011-02-23 10:19:14

回答

2

嘿德维尔,有一个稍微不同的方法,你应该在这里拍摄。这是我的建议。

.h文件中的代码

#import <UIKit/UIKit.h> 

    @interface trialViewController : UIViewController <UIActionSheetDelegate, UITextFieldDelegate> { 

     UITextField *TextField; 

    } 

    -(void) CreateSlideOut; 
    -(void) removePopUp; 
    -(void) slidePopup; 
    @end 

.M码

#import "trialViewController.h" 

@implementation trialViewController 


CGRect backToOriginal; 
UIView *popup; 

- (void)viewDidLoad { 

    [self CreateSlideOut]; 
    [self slidePopup]; 
    [super viewDidLoad]; 
} 


-(void) CreateSlideOut 
{ 

    CGRect frame=CGRectMake(0, CGRectGetMaxY(self.view.bounds), 320, 480); 

    backToOriginal=frame; 

    popup=[[UIView alloc]initWithFrame:frame]; 

    popup.backgroundColor = [UIColor orangeColor]; 

    [self.view addSubview:popup]; 

} 


-(void) slidePopup 
{ 

    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame=CGRectMake(60, 190, 200,40); 
    button.backgroundColor=[UIColor clearColor]; 

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 

    [button setTitle:@"Dismiss" forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(DismissClicked:) forControlEvents:UIControlEventTouchUpInside]; 

    TextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 28, 280, 150)]; 
    TextField.delegate=self; 
    TextField.autocorrectionType = UITextAutocorrectionTypeNo; 
    [TextField setBackgroundColor:[UIColor clearColor]]; 
    [TextField setBorderStyle:UITextBorderStyleRoundedRect]; 

    [popup addSubview:TextField]; 
    [popup addSubview:button]; 

    CGRect frame=CGRectMake(0, 0, 320, 480); 

    [UIView beginAnimations:nil context:nil]; 

    [popup setFrame:frame]; 

    [UIView commitAnimations]; 


} 

-(void) removePopUp 

{ 
    [UIView beginAnimations:nil context:nil]; 

    [popup setFrame:backToOriginal]; 

    [UIView commitAnimations]; 

    [TextField resignFirstResponder]; 
} 

-(IBAction) DismissClicked : (id) sender 
{ 

    [self removePopUp]; 
} 

- (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)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    NSLog(@"in textFieldShouldReturn"); 
    [TextField resignFirstResponder]; 
    return YES; 
} 

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

@end 

使用它,改变滑出式的背景颜色。如果你想我可以给一个代码来定制你的UIButtons并为你使用创建自定义颜色。干杯

0

只有关键窗口接收到键盘通知,所以你必须将你的textField添加到关键窗口。

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 28, 320, 150)]; 
// Configure your textField here: 

UIWindow *appWindow = [UIApplication sharedApplication].keyWindow; 
[appWindow addSubview:textField]; 
相关问题