如何隐藏和取消隐藏UIAlertView。

时间:2015-07-29 05:47:46

标签: ios uitextfield uialertview

我将在视图出现时打开警报视图。我的alertview样式是UIAlertViewStylePlainTextInput。我正在NSUserDefaults中保存文本字段文本。我希望当textfield文本域中的文本没有打开但是如果textfield为空,那么屏幕上会弹出警告。我正在使用下面的代码。enter code here

- (void)viewDidLoad {
    [super viewDidLoad];
       proAlert = [[UIAlertView alloc]initWithTitle:@"Pro-Tracking Number" message:@"Firstly enter the protracking number here" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
    proAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [proAlert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0)
    {
        proTextField.text = [[proAlert textFieldAtIndex:0]text];
    }

}
 proTextField = [[UITextField alloc]initWithFrame:CGRectMake(170, 35, 150, 40)];
    proTextField.textColor=[UIColor blackColor];
    //proTextField.placeholder = @"Pro/Tracking no";
    NSUserDefaults *proNum = [NSUserDefaults standardUserDefaults];
    proTextField.text = [proNum valueForKey:@"proTracking"];
    [view2 addSubview:proTextField];
-(void)viewWillAppear:(BOOL)animated
{
    [activity stopAnimating];

    NSString *textString =[[proAlert textFieldAtIndex:0]text];

    [proTextField.text length];


    myText =textString;
    NSLog(@"Textfield text - %@",myText);
    NSUInteger length = [myText length];



    NSLog(@"LENGTH of string %lu",(unsigned long)length);

   if(myText<0)
   {
       proAlert.hidden = NO;
         }
    else
    {
        proAlert.hidden = YES;
    }
}

1 个答案:

答案 0 :(得分:3)

-(void)viewWillAppear:(BOOL)animated {
    if (txtField.text.length<=0) {
       proAlert = [[UIAlertView alloc]initWithTitle:@"Pro-Tracking Number" message:@"Firstly enter the protracking number here" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
       proAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
      [proAlert show];
    }
}