如何在UIActionsheet中使用UITextFields?

时间:2013-02-12 04:51:34

标签: iphone ios uiactionsheet

我是iphone的新手。我正在为我的项目做提醒申请。我希望得到输入值,如提醒标题和数据&时间uiactionsheet.i已在动作表中创建了datepicker和uitextfield。操作表出现在textfield中。当我点击文本字段时,键盘出现但不起作用。当我输入时,它们在文本字段中不可见

-(void)btnSetRemainderTapped:(id)sender
{
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Select reminder date and time"
                                                  delegate:self
                                         cancelButtonTitle:@"Cancel"
                                    destructiveButtonTitle:@"Save"
                                         otherButtonTitles:nil,nil];
 menu.tag =10;
[menu showInView:self.view];
menu.userInteractionEnabled=YES;

UITextField  *txtReminderTitle= [[UITextField alloc] initWithFrame:CGRectMake(20.0, 180, 280.0, 40.0)];
[txtReminderTitle setTag:99];
[txtReminderTitle setBackgroundColor:[UIColor whiteColor]];
[txtReminderTitle setFont:[UIFont boldSystemFontOfSize:17]];
[txtReminderTitle setBorderStyle:UITextBorderStyleNone];
[txtReminderTitle setTextAlignment:UITextAlignmentCenter];
txtReminderTitle.borderStyle = UITextBorderStyleRoundedRect;
txtReminderTitle.keyboardType = UIKeyboardTypeDefault;
txtReminderTitle.returnKeyType = UIReturnKeyDone;
txtReminderTitle.delegate =self;
[menu addSubview:txtReminderTitle];

UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDateAndTime;
pickerView.tag =9999;
[menu addSubview:pickerView];

CGRect menuRect = menu.frame;
NSInteger orgHeight = menuRect.size.height;
menuRect.origin.y -= 270; //height of picker
menuRect.size.height = orgHeight+270;
menu.frame = menuRect;

CGRect pickerRect = CGRectMake(20, 250, 280, 200);
pickerView.frame = pickerRect;

}

-(void) actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
  {
UIDatePicker *datePicker = (UIDatePicker *)[actionSheet viewWithTag:9999];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterLongStyle;
[df setTimeStyle:NSDateFormatterShortStyle];
UITextField * txtReminderTitle = (UITextField *)[actionSheet viewWithTag:99];
   NSLog(@"%@",txtReminderTitle.text);  //it print null value
  }

我不确定为什么它不接受键盘的输入。有什么建议吗?

2 个答案:

答案 0 :(得分:4)

我发现,当您将UITextField添加到UIActionSheet时,UITextField会保留在UIActionSheet下。因此,您需要将UITextField置于UIActionSheet之上 [menu addSubview:txtReminderTitle];不适用于UITextField 输入任何文字

使用以下代码添加UITextField

UIWindow *appWindow = [UIApplication sharedApplication].keyWindow;
[appWindow insertSubview:txtReminderTitle aboveSubview:menu];

只需要对如何添加UITextField进行更改。

答案 1 :(得分:0)

使用自定义UIActionSheet。喜欢以下
1)添加新的UIViewController
2)声明一个UITextField

@interface ViewController  
{
            UITextField *TextField;

    }

3)添加ActionSheet&的代表的TextField

@interface ViewController : UIViewController <UIActionSheetDelegate, UITextFieldDelegate> {

        UITextField *TextField;

    }

4)现在实现方法

stackoverflow

上看到这个链接