I am working on an app. I tried to add an UIIMage to UIAlertView using below code,
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Instruction"
message:#"Please TAP on Screen to Continue."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 50, 32, 32)];
UIImage *img = [UIImage imageNamed:#"pendingImg.png"];
[imageView setImage:img];
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
[alert setValue:imageView forKey:#"accessoryView"];
}else{
[alert addSubview:imageView];
}
[alert show];
My image is of dimension 32 × 32 pixels. I am getting the alert as shown,
Should I add constraints to this image? or anything else to be made?
You can set imageView contentMode to UIViewContentModeScaleAspectFit,
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Instruction"
message:#"Please TAP on Screen to Continue."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 50, 32, 32)];
UIImage *img = [UIImage imageNamed:#"pendingImg.png"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[imageView setImage:img];
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
[alert setValue:imageView forKey:#"accessoryView"];
}else{
[alert addSubview:imageView];
}
[alert show];
Might be Anbu.Karthik suggested link comment also helpful for you.
try this
UIAlertView* alert = [UIAlertView alloc] initWithTitle: #"Instruction" message: #"Please TAP on Screen to Continue." delegate:nil cancelButtonTitle:#"no" otherButtonTitles:#"yes", nil];
UIImage* img = [UIImage imageNamed:#"pendingImg.png"];
UIImageView* imgview = [UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
[img setImage:imgMyImage];
[alert setValue: imgview forKey:#"accessoryView"];
[alert show];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle: #"Instruction" message: #"Please TAP on Screen to Continue." delegate:nil cancelButtonTitle:#"no" otherButtonTitles:#"yes", nil];
UIImage* img = [UIImage imageNamed:#"add.png"];
UIImageView* imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
[imgview setImage:img];
[alert setValue: imgview forKey:#"accessoryView"];
[alert show];
}
Related
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.
I want to do validation.
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.
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.
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.
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....
}
I am new in IOS. When I'm clicking on the following Login Button:
I'm using this code to generate a popup:
- (UIView *)createLoginView
{
UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 130)];
UITextField *email = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 280, 50)];
email.tag=420;
email.layer.cornerRadius = 5;
[email setTag:99];
[email setTextAlignment:NSTextAlignmentCenter];
[email setKeyboardType:UIKeyboardTypeEmailAddress];
[email setPlaceholder:#"Email"];
[email setBackgroundColor:[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0]];
[email setReturnKeyType:UIReturnKeyNext];
[email setAutocorrectionType:UITextAutocorrectionTypeNo];
UITextField *pass = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, 280, 50)];
pass.layer.cornerRadius = 5;
[pass setTag:100];
[pass setTextAlignment:NSTextAlignmentCenter];
[pass setSecureTextEntry:YES];
[pass setPlaceholder:#"Password"];
[pass setBackgroundColor:[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0]];
[pass setReturnKeyType:UIReturnKeyGo];
[alertView addSubview:email];
[alertView addSubview:pass];
return alertView;
}
The login popup is working properly but when I click on the 'Forget?' button of the popup, the forgot password popup does not show up. Here is the code for forget password popup.
- (UIView *)createForgotPasswordView
{
UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 130)];
UITextField *email = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 280, 50)];
email.layer.cornerRadius = 5;
[email setTag:22];
[email setTextAlignment:NSTextAlignmentCenter];
[email setKeyboardType:UIKeyboardTypeEmailAddress];
[email setPlaceholder:#"Email"];
[email setBackgroundColor:[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0]];
[email setReturnKeyType:UIReturnKeyNext];
[email setAutocorrectionType:UITextAutocorrectionTypeNo];
[alertView addSubview:email];
return alertView;
}
And I'm using it like :
- (IBAction)LogInClick:(UIButton *)sender {
IOS7AlertView *applyMsg = [[IOS7AlertView alloc] init];
[applyMsg setContainerView:[self createLoginView]];
[applyMsg setButtonTitles:[NSMutableArray arrayWithObjects:#"Forgot?", #"Login", nil]];
[applyMsg setOnButtonTouchUpInside:^(IOS7AlertView *alertView, int buttonIndex) {
if(buttonIndex==0)
{
[alertView close];
NSLog(#"Forgot Section");
IOS7AlertView *applyPop = [[IOS7AlertView alloc] init];
[applyPop setParentView:[self createForgotPasswordView]];
[applyPop setButtonTitles:[NSMutableArray arrayWithObjects:#"Next", #"Cancel", nil]];
}
}];
But the problem is the next popup does not show up? What is the problem? Please help.
already UIalertview hierarchy has email and password options.
try this and this is native
- (IBAction)LogInClick:(UIButton *)sender {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Login"
message:nil
delegate:self
cancelButtonTitle:#"Signup"
otherButtonTitles:#"Login",#"Forgot password", nil];
alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;
[[alert textFieldAtIndex:0]setKeyboardType:UIKeyboardTypeEmailAddress];
[[alert textFieldAtIndex:0]setPlaceholder:#"Email address"];
[[alert textFieldAtIndex:1]setPlaceholder:#"Password"];
alert.tag=10;
[alert show];
}
and the alert view delegate method is
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag==65)
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Login"
message:nil
delegate:self
cancelButtonTitle:#"Signup"
otherButtonTitles:#"Login",#"Forgot password", nil];
alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;
[[alert textFieldAtIndex:0]setKeyboardType:UIKeyboardTypeEmailAddress];
[[alert textFieldAtIndex:0]setPlaceholder:#"Email address"];
[[alert textFieldAtIndex:1]setPlaceholder:#"Password"];
alert.tag=10;
[alert show];
}
if (alertView.tag==10)
{
if (buttonIndex == 0) {
NSLog(#"signup");
//Navigate to Signup page
}
else if (buttonIndex == 2) {
NSLog(#"Forgot password");
// //Navigate to Forgot password page
}
else if (buttonIndex == 1) {
NSLog(#"YES");
NSString *usernameInput=[alertView textFieldAtIndex:0].text;
NSString *passwordInput=[alertView textFieldAtIndex:1].text;
//pass the email id , password
NSLog(#"1 %#", usernameInput);
NSLog(#"2 %#", passwordInput);
NSString *emailReg = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailReg];
// COMMENT: check if input information is valid or not
if([usernameInput isEqualToString:#""])
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert!!!" message:#"You must fill in Email address" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
alert.tag=65;
[alert show];
}
else if([emailTest evaluateWithObject:usernameInput] == NO)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert!!" message:#"Please Enter Valid Email Address." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag=65;
[alert show];
}
else if([passwordInput isEqualToString:#""])
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert!!!" message:#"You must fill in Password" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
alert.tag=65;
[alert show];
}
else
{
//pass teh data to server or login process
}
}
}
Can we programmatically insert text in UIAlertView's textfield, iOS7
Created UIAlertView with textinput in IOS7.
Can anyone help me with "inserting text programatically in textfield"?
Hopefully this will give answer for your question.
UIAlertView *alertView = [[UIAlertView alloc] init];
alertView.delegate = self;
alertView.title = #"Enter Info";
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView addButtonWithTitle:#"Cancel"];
[alertView addButtonWithTitle:#"OK"];
[alertView textFieldAtIndex:0].text = #"My Text";
[alertView show];
You can achieve this by using this code
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"" message:#"whatever you like" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:#"Cancel", nil];
alert.alertViewStyle=UIAlertViewStylePlainTextInput;
[alert textFieldAtIndex:0].text=#"Hello";
[alert show];
and result will be like this:
Please try to use like this..
UIAlertView* av = [[UIAlertView alloc] init];
[av setDelegate:self];
[av setTitle:#"Hi"];
[av setMessage:nil];
[av addButtonWithTitle:#"Cancel"];
[av addButtonWithTitle:#"OK"];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
[av textFieldAtIndex:0].text = #"Text";
You can use text property to set text inside textfield.
textFiled.text = #"YOUR TEXT";
This question already has answers here:
UIAlertView addSubview in iOS7
(8 answers)
Closed 9 years ago.
I am trying to add an image in a UIAlertView in this way:
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Accept Connection?"
message:[NSString stringWithFormat:#"%# wants to connect with you", newStr]
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 100, 50)];
NSString *imgPath = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"BluetoothDeviceiPhone.png"]];
UIImage *yourImage = [[UIImage alloc] initWithContentsOfFile:imgPath];
[imageView setImage:yourImage];
[alertView addSubview:imageView];
[alertView show];
alertView.tag = 2;
The image is not showing though, neither a space is showing, just the text. Why??
check this sample code it also running in ios7 and prior
https://github.com/wimagguc/ios-custom-alertview
I am trying to play an animation of images in a UIAlertView.
Here is my code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
UIImageView *animation = nil;
animation.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"Comment-Edit-48.png"],[UIImage imageNamed:#"Share-48.png"],[UIImage imageNamed:#"Comment-Edit-48.png"],[UIImage imageNamed:#"Play-48.png"], nil];
[animation setAnimationRepeatCount:10];
animation.animationDuration = 1.5;
[animation startAnimating];
[alert addSubview:animation];
[alert show];
[alert release];
Can anyone please tell me what I did wrong here ?
This is due to your animation imageView frame size. You need to alloc it with certain frame size of your animation imageView. If you don't do that then it will not occupy any memory space. A nil object would have been add inside UIAlertView. Try this below given code
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
UIImageView *animation =[[UIImageView alloc] initWithFrame:CGRectMake(120, 50, 50, 45)];
animation.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"Comment-Edit-48.png"],[UIImage imageNamed:#"Share-48.png"],[UIImage imageNamed:#"Comment-Edit-48.png"],[UIImage imageNamed:#"Play-48.png"], nil];
[animation setAnimationRepeatCount:10];
animation.animationDuration = 1.5;
[animation startAnimating];
[alert addSubview:animation];
[alert show];
[alert release];
Set this frame size and it works fine. I hope it will help you. Thanks