ios - 如何在 Objective C 中检查 UIView 是否为零

标签 ios objective-c uiview

我是 iOS 新手,在验证 View 方面遇到问题。我创建了类似签名的 View How to draw Signature on UIView ,现在我想使用此代码进行验证

-(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];
    }
}

但我没有取得成功。请告诉我我做错了什么。

enter image description here

我想做验证。

enter image description here

如果我没有签名,则会显示该消息。

请给我建议。

提前致谢!

我保存图像和使用条件

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];
    }

但它不起作用,因为它包含空白图像。

最佳答案

 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];
 }

尝试一下这段代码,它绝对有效。

关于ios - 如何在 Objective C 中检查 UIView 是否为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41518435/

相关文章:

javascript - 当应用程序在后台运行时,响应 native 电源按钮长按监听器

ios - PJSIP 不响应 401 的身份验证

objective-c - UIScrollView 不会滚动

iphone - NSUserDefaults 记住 View

ios - 解析.com : saveInBackgroundWithBlock - block not called

iphone - 与 UIGestureRecognizer 一起监听 UITouch 事件

ios - 使用 Storyboard推送 viewController 时传递条件语句

iphone - 加载 View 时获取 NULL IBOutlets 的可能性是什么.. IOS

iphone - 获取自定义 UIView 子类以跟随 UIControl

objective-c - 来自 UIView 的图像