Set background from UIAlertView option xcode - ios

Im trying to do an app with 4 ImageViews and 4 UIButtons. When one of the UIButtons are pressed a UIAlertView should appear with 3 options. One named "Picture" should open the photo library so the user can change the ImageView picture. I have wrote a code for this but it wont work. Do anyone have any suggestions on how to make it work?
Thanks in advance!
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
NSString *buttonOne = changeButtonOne; //the UIButtons
NSString *buttonTwo = changeButtonTwo;
NSString *buttonThree = changeButtonThree;
NSString *buttonFour = changeButtonFour;
if([buttonOne isEqualToString:#"buttonOne"])
{
if([title isEqualToString:#"Done"])
{
NSLog(#"You pressed done");
}
else if([title isEqualToString:#"Phone number"])
{
NSLog(#"You pressed Phone number");
}
else if([title isEqualToString:#"Picture"])
{
imagePickerController = [[UIImagePickerController alloc]init]; //I think its this part which are wrong
[imagePickerController setDelegate:self];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController animated:YES completion:nil];
}
}
else if([buttonTwo isEqualToString:#"buttonTwo"])
{
if([title isEqualToString:#"Done"])
{
NSLog(#"Yoy pressed done");
}
else if([title isEqualToString:#"Phone number"])
{
NSLog(#"You pressed Phone number");
}
else if([title isEqualToString:#"Picture"])
{
imagePickerController2 = [[UIImagePickerController alloc]init]; //I think its the part thats wrong.
[imagePickerController2 setDelegate:self];
[imagePickerController2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController2 animated:YES completion:nil];
}
}
else if([buttonThree isEqualToString:#"buttonThree"])
{
if([title isEqualToString:#"Done"])
{
NSLog(#"You pressed done");
}
else if([title isEqualToString:#"Phone number"])
{
NSLog(#"You pressed Phone number");
}
else if([title isEqualToString:#"Picture"])
{
imagePickerController3 = [[UIImagePickerController alloc]init]; //i think this is the wrong part
[imagePickerController3 setDelegate:self];
[imagePickerController3 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController3 animated:YES completion:nil];
}
}
else if([buttonFour isEqualToString:#"buttonFour"])
{
if([title isEqualToString:#"Done"])
{
NSLog(#"You pressed done");
}
else if([title isEqualToString:#"Phone number"])
{
NSLog(#"You pressed Phone number");
}
else if([title isEqualToString:#"Picture"])
{
imagePickerController4 = [[UIImagePickerController alloc]init]; //this is probably the wrong part
[imagePickerController4 setDelegate:self];
[imagePickerController4 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController4 animated:YES completion:nil];
}
}
}
#end

You should use the tag property of UIAlertView to differentiate between the alerts and the buttonIndex (not the button title) to know which button was pressed.
So don't use isEqualToString at all in the alert view delegate method. You want this to work with different languages.
In the button action, before you show the alert:
alertView.tag = 1; // 1 = button1, 2 = button2 etc.
Alert view delegate:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1) { //button1
if (buttonIndex == alertView.cancelButtonIndex) {
// handle cancel button
}
if (buttonIndex == alertView.firstOtherButtonIndex) {
// handle action 1
}
if (buttonIndex == alertView.firstOtherButtonIndex+1) {
// handle action 2
}
}
if (alertView.tag == 2) { //button2
if (buttonIndex == alertView.cancelButtonIndex) {
// handle cancel button
}
if (buttonIndex == alertView.firstOtherButtonIndex) {
// handle action 3
}
if (buttonIndex == alertView.firstOtherButtonIndex+1) {
// handle action 4
}
}
}

Related

How to turn off dismiss UIAlertView when click UIButton in alert view

What is correct way to turn off dismiss UIAlertView when click button in UIAlertView. I want to check if not have string in UITextField when click button not dismiss the view.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//Add alert delegate
if (alertView.tag == kDeleteAlertTag)
{
if (buttonIndex == 0)
{
[self.emails removeObjectAtIndex:currentIndex];
[self.plistReader saveToPlistName:kPlistEmails fromArray:self.emails];
[self.tblEmails reloadData];
}
}
// remove email alert delegate
if (alertView.tag == kAddEmailTag)
{
if (buttonIndex == 0)
{
// get text Input Alert
UITextField * emailInputTextField = [alertView textFieldAtIndex:0];
// get string from text Alert
NSString *textInput = [emailInputTextField text];
if (self.emails== nil) {
self.emails = [[NSMutableArray alloc]init];
}
if ([textInput isEqualToString:#""]) {
NSLog(#"Not have string");
}else
if ([KUtils NSStringIsValidEmail:textInput] == YES)
{
[self.emails addObject:textInput];
// save to plist and reload table
[self.plistReader saveToPlistName:kPlistEmails fromArray:self.emails];
[self.tblEmails reloadData];
}else if([KUtils NSStringIsValidEmail:textInput] == NO)
{
// not dismiss alert in hre
}
}
}
Don't validate against the string validate against the text field
if (emailInputTextField.text && emailInputTextField.text.length > 0)
{
//do something
}
else
{
//All fields are required UIAlert here
}
Edit
You can also try:
if ([emailInputTextField.text length] != 0) {
//Do something
}
else {
//All fields are required UIAlert here
}

UIAlertView not calling

I'm attempting to create a simple webpage browser-esque application, where whenever the user wants to use a back, home, or forward function they are asked through an alert view to confirm.
The console window confirms that it has no problem calling the IBAction but comes up blank whenever I expect it to call Alert View.
Any help would be appreciated.
- (IBAction)controlPanelActions:(id)sender{
if (_controlPanel.selectedSegmentIndex == 0)
{
if ([_viewWeb canGoBack])
{
[self askToGoHome:nil];
}
}
- (IBAction)askToGoHome:(id)sender {
UIAlertView *alertDialog;
alertDialog.tag = 2;
alertDialog = [[UIAlertView alloc]
initWithTitle: #"Confirm"
message:#"Continue with Home Action?"
delegate: self
cancelButtonTitle: #"Yes"
otherButtonTitles: #"No", nil];
[alertDialog show];
}
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if (alertView.tag == 1)
{
if ([buttonTitle isEqualToString:#"Yes"])
{
NSLog(#"back - yes");
[_viewWeb goBack];
}
}
if (alertView.tag == 2)
{
NSLog(#"home - pre-yes");
if ([buttonTitle isEqualToString:#"Yes"])
{
NSLog(#"home - yes");
HOMEPAGEURL = [[NSURL alloc] initWithString:HOMEPAGE];
[self.viewWeb loadRequest:[NSURLRequest requestWithURL:HOMEPAGEURL]];
}
}
if (alertView.tag == 3)
{
if ([buttonTitle isEqualToString:#"Yes"])
{
[_viewWeb goForward];
}
}
}
You set alertDialog.tag = 2;before you call init.
So,everytime you set tag,you set tag to a nil.It will not work.
Alertview alloc and init method sets its tag to 0.
So alertDialog.tag = 2;
will not work.
add this line after alloc and init method.

Giving function for more uiSwitch tags

I would like to set that if all of my switches for sender tag 0-6 will be off, app shows alert with warning message to put at least one of the switches on. I am now little bit lost with logic how to.
if (switi.tag ==0 && switi.isOn == YES )
{ [self.navigationController popViewControllerAnimated:YES];
[self willMoveToParentViewController:nil];
}else if ((switi.tag ==1 && switi.isOn == YES)) {
[self.navigationController popViewControllerAnimated:YES];
[self willMoveToParentViewController:nil];
} else if ((switi.tag ==2 && switi.isOn == YES)) {
[self.navigationController popViewControllerAnimated:YES];
[self willMoveToParentViewController:nil];
} else if ((switi.tag ==3 && switi.isOn == YES)) {
[self.navigationController popViewControllerAnimated:YES];
[self willMoveToParentViewController:nil];
} else if ((switi.tag ==4 && switi.isOn == YES)) {
[self.navigationController popViewControllerAnimated:YES];
[self willMoveToParentViewController:nil];
} else if ((switi.tag ==5 && switi.isOn == YES)) {
[self.navigationController popViewControllerAnimated:YES];
[self willMoveToParentViewController:nil];
} else if ((switi.tag ==6 && switi.isOn == YES)) {
[self.navigationController popViewControllerAnimated:YES];
[self willMoveToParentViewController:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please put at least one switch on." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
EDIT
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat y = 110;
int counter = 0;
for (NSString *key in self.kejsss) {
UILabel *labeli = [[UILabel alloc] initWithFrame:CGRectMake(50, y, 150, 30)];
UISwitch *switi = [[UISwitch alloc] initWithFrame:CGRectMake(210, y, 70, 30)];
labeli.tag = counter;
switi.tag = counter;
labeli.text = key;
labeli.textColor = [UIColor whiteColor];
switi.on = [[self.konfiggg objectForKey:key] boolValue];
[switi addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:labeli];
[self.view addSubview:switi];
self.viewtabulka.layer.borderColor = [UIColor whiteColor].CGColor;
self.viewtabulka.layer.borderWidth = 0.5;
CALayer *imageLayerRR3 = self.viewtabulka.layer;
[imageLayerRR3 setCornerRadius:5];
[imageLayerRR3 setBorderWidth:1];
[imageLayerRR3 setMasksToBounds:YES];
y += 40;
counter++;
}
}
}
}
- (IBAction)switchChanged:(UISwitch *)sender{
kokos= YES;
[self.konfiggg setObject:#([sender isOn]) forKey:[self.kejsss objectAtIndex:sender.tag]];
NSLog(#"Selected Switch - %ld (%#)", (long)sender.tag, self.kejsss[sender.tag]);
NSLog(#"Selected object - %d", [sender isOn]);
}
Here is one way. Group all of your switches together in an array for convenience. Then whenever one of the switches changes value, call a method that loops through all of the switches in the array and checks to make sure at least one is still on. You could do it like this:
-(void)checkSwitches {
// self.switches is an NSArray that holds pointers to all of your UISwitches
for (UISwitch *s in self.switches) {
if (s.on == YES) {
return;
}
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please put at least one switch on." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}

iOS AlertView trigger which button has been pressed

I am trying to see what option the client has pushed on the alert view button, though the logging doesn't appear to be working
-(IBAction)flattenBtn:(id)sender
{
UIAlertView* flatView = [[UIAlertView alloc] initWithTitle:#"Flatten Image"
message:#"Are you sure you want to flatten your image?" delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Yes",#"No",nil];
[flatView show];
[flatView release];
}
- (void)flatView:(UIAlertView *)flatView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"YES");
}
else if (buttonIndex == 1)
{
NSLog(#"NO");
}
}
Try it using like this :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Yes"])
{
//your code if YES pressed;
}
else
{
//your code if NO pressed;
}
}

MBProgressHUD after selecting image in UIImagePickerController

I'm selecting an image in UIImagePickerController.
After the image was selected I'm firing another UIActionSheet to select from
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
_selected_image = info[UIImagePickerControllerEditedImage];
UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:
#"Share this photo",
nil];
popup.tag = 2;
[popup showInView:[UIApplication sharedApplication].keyWindow];
}
When the user selects "Share" a long process is starting.
I want the MBProgressHUD to show during the process. I can't get progress HUD to show before stuff are starting to happen.
Tried two things:
- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {
if (popup.tag == 1)
{
...
}
else if (popup.tag == 2)
{
if (buttonIndex == 1) //cancel
{
_selected_image = nil;
}
else if (buttonIndex == 0)
{
[imagePicker dismissViewControllerAnimated:YES completion:NULL];
[self doSharePhoto];
return;
}
[imagePicker dismissViewControllerAnimated:YES completion:NULL];
}
}
doSharePhotos start running when the imagePicker is still showing.
Tried:
- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {
if (popup.tag == 1)
{
}
else if (popup.tag == 2)
{
if (buttonIndex == 1) //cancel
{
_selected_image = nil;
}
else if (buttonIndex == 0)
{
[imagePicker dismissViewControllerAnimated:YES completion:^{
[self doSharePhoto];
}];
return;
}
[imagePicker dismissViewControllerAnimated:YES completion:NULL];
}
}
and in this case the MBProgressHUD inside doSharePhoto doesn't apprear at all
** EDIT **:
The code that starts the HUD is in [self doSharePhoto]:
- (void)doSharePhoto {
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
HUD.labelText = #"Please wait...";
HUD.delegate = self;
...
}
Any thoughts?
Does your doSharePhoto method perform the long running operation inside a seperate thread? If not it's blocking the main thread so , MBProgressHUD doesn't get a chance to show. Your doSharePhoto must look like this,
- (void)doSharePhoto {
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Do the long running operation here...
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
});
});
}
EDIT: Problem might be with the view you add your HUD to,
Change
self.navigationController.view to
self.navigationController.visibleViewController.view
So,
[MBProgressHUD hideHUDForView:self.navigationController.visibleViewController.view animated:YES];

Resources