How to check UIView is nil or not in objective c

2019-07-23 04:12发布

I am new in iOS and I am facing problem regarding to do validation of view. I have created Signature like view How to draw Signature on UIView, and now I want to do validation by using this code

-(IBAction)SavebtnClick:(id)sender
{

    if(drawSignView==nil)
    {
       UIAlertView *alertviewshow =[[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Pease Sign" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alertviewshow show];
    }
    else if (drawSignViewClient==nil)
    {
        UIAlertView *alertviewshow =[[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Please Sign" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alertviewshow show];
    }
    else
    {

        viewcontroller1 *management =[[viewcontroller1 alloc] initWithNibName:@"viewcontroller1" bundle:nil];
        [self.navigationController pushViewController:management animated:YES];
    }
}

But I am not getting success. Please tell me what I am doing wrong.

enter image description here

I want to do validation.

enter image description here

if I have not sign it shows the message.

Please give me suggestion.

Thanks in advance!

I save Image and used condition

UIGraphicsBeginImageContext(self.drawSignView.bounds.size);
    [[self.drawSignView.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0);


    // Store the data
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];


    NSData *imageData = UIImageJPEGRepresentation(viewImage, 100);
    [defaults setObject:imageData forKey:@"image"];

    [defaults synchronize];


    if(viewImage==nil)
    {
        UIAlertView *alertviewshow =[[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Pease Sign" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alertviewshow setTag:1];
        [alertviewshow show];
    }

But it not work because it contain blank image.

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-07-23 04:33
 UIGraphicsBeginImageContextWithOptions(self.drawSignView.bounds.size, NO, [UIScreen   mainScreen].scale);

 [self.drawSignView drawViewHierarchyInRect:self.drawSignView.bounds afterScreenUpdates:YES];
 UIImage *signImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

if(signImage)
  {
    //Get Image from sign view Succesfully
  }
 else
 {
   UIAlertView *alertviewshow =[[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Please Sign" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alertviewshow show];
 }

Try this code its definatly work.

查看更多
混吃等死
3楼-- · 2019-07-23 04:48

Please check this answer

    UIGraphicsBeginImageContext(self.drawSignView.bounds.size);
    [[self.drawSignView.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0);

    NSData *imgData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((viewImage), 0.5)];
    int imageSize   = imgData.length;
    NSLog(@"size of image in KB: %d ", imageSize/1024);



    UIGraphicsBeginImageContext(self.drawSignViewClient.bounds.size);
    [[self.drawSignViewClient.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImageClient = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0);

    NSData *imgDataClient = [[NSData alloc] initWithData:UIImageJPEGRepresentation((viewImageClient), 0.5)];
    int imageSizeClient   = imgDataClient.length;
    NSLog(@"size of image in KB: %d ", imageSizeClient/1024);

    int OCS=imageSize/1024;
    int Client=imageSizeClient/1024;

    if(OCS<3)
    {
        alertviewshow=[[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Please do Signature " delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alertviewshow setTag:1];
        [alertviewshow show];
    }
    else if (Client<3)
    {
        alertviewshow=[[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Please do Signature " delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alertviewshow setTag:1];
        [alertviewshow show];
    }
else
{
//Write Your Code....
}
查看更多
Anthone
4楼-- · 2019-07-23 04:49

For that my suggestion is to check UIImage rather then UIView, That means are you getting the signature image or not. Then you should check like

if(singatureImage1 == nil){
}
else{
}

And for another signature

if(singatureImage2 == nil)
{

}
else{
}

And if you getting not nil image without sign it then use SinatureView which will give you nil image if you did't sign it then you can validate it.

If you check the UIView(i.e. Your signature view) is nil or not,You will get always not nil view because you have initialized it.

查看更多
登录 后发表回答