Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have an if statement that is working fine, but I need to add a 2nd if statement inside of it and I can't seem to figure out how to get it right.
Here is my code:
-(IBAction)xButton {
if([_hasUserTakenAPhoto isEqual: #"YES"]) {
_xButtonAfterPhotoTaken = [[UIActionSheet alloc] initWithTitle:#"Delete" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
[_xButtonAfterPhotoTaken showInView:self.view];
NSString *title = [_xButtonAfterPhotoTaken buttonTitleAtIndex:1];
if(title isEqualToString:#"Delete") {
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
} else {
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
}
I get errors when I add in the 2nd if statement of:
if(title isEqualToString:#"Delete") {
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
I have tried making the 2nd if statement an "else if" but then it will not let me access the NSString object called "title". Is there an easier way to do this, or should I just make title a global variable?
UIActionSheets are not used that way:
- (IBAction)xButton:(UIButton*)sender
{
if ([_hasUserTakenAPhoto isEqual:#"YES"])
{
_xButtonAfterPhotoTaken = [[UIActionSheet alloc] initWithTitle:#"Delete Photo?" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Delete" otherButtonTitles:nil];
[_xButtonAfterPhotoTaken showInView:self.view];
}
else
{
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Check if it's the correct action sheet and the delete button (the only one) has been selected.
if (actionSheet == _xButtonAfterPhotoTaken && buttonIndex == 0)
{
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
}
- (void)actionSheetCancel:(UIActionSheet *)actionSheet
{
NSLog(#"Canceled");
}
You gotta understand that interface elements are not "instant", there's plenty of asynchrony going on. When presenting an UIActionSheet for instance, the thread doesn't wait for the user to answer yes or no, it keeps running.
That's why there's delegates, and blocks, you present the UIActionSheet, and with the delegate you say "I'll take care of it when the user actually clicks it".
You'd be wondering, why not just wait for it to select it? Main thread takes care of updating the interface, animations, and retrieving user input (touches, keyboard taps, etc) and even running NSTimers that are subscript to the main NSRunLoop. Stopping main thread would lock the interface.
Try
- (IBAction)xButton
{
NSString *title;
if ([_hasUserTakenAPhoto isEqual:#"YES"])
{
_xButtonAfterPhotoTaken = [[UIActionSheet alloc] initWithTitle:#"Delete" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
[_xButtonAfterPhotoTaken showInView:self.view];
title = [_xButtonAfterPhotoTaken buttonTitleAtIndex:1];
if ([title isEqualToString:#"Delete"])
{
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
}
else
{
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
}
Related
I have a method which throws up an actionSheet which is populated by an array.
The problem is that as soon as I place that method in the sender for 1 button, it fires off for every single button press.
I only want it to display for the one button (btnPortfolio).
Here is the code for my buttons:
- (IBAction)btnPortfolio:(id)sender {
[self populatePortfolioList];
}
- (IBAction)btnAdd:(id)sender {
}
- (IBAction)btnRefresh:(id)sender {
}
and here is my method:
-(void)populatePortfolioList{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Choose your option"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (NSString *title in portfolio_list) {
[actionSheet addButtonWithTitle:title];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"Cancel"];
[actionSheet showInView:self.view];
}
since you are using IBActions, I assume that you have somehow created the methods by ctrl+dragging from the buttons into your code? is that the case? it might be possible that you accidentally connected more than one of your buttons with the method btnPortfolio which causes populatePortfolioList to be called any time the button is pressed
There is an issue with an UIAlertView when i add to this a lot of buttons. Then the alertView seems to be destroyed. This happens only to prior version of iOS 7. On iOS 7 and posterior versions it seems ok.Here is a screenshot of my problem.Can i fix it?
- (void) sortTotalMnhmeia{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Ταξινόμηση" message:#"Επιλέξτε είδος ταξινόμησης" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Αξιοθέατα",#"Δραστηριότητες",#"Διαμονή",#"Χωριά",#"Προϊόντα",#"Όλες οι κατηγορίες",nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{
NSLog(#"Cancel Tapped.");
}
else if (buttonIndex == 1)
{
[self.mapView removeAnnotations:self.annotations];
[self.annotations removeAllObjects];
self.annotations=[[NSMutableArray alloc] init];
NSLog(#"yo %d",self.annotations.count);
for(int i=0; i<self.allGroups.count; i++){
Group *assistantGroup=assistantGroup=[self.allGroups objectAtIndex:i];
if ([assistantGroup.secondLevel intValue]==1) {
if ([assistantGroup.thirdLevel intValue]==1) {
self.chooseMarker=#"Museum";
}
else if ([assistantGroup.thirdLevel intValue]==2) {
self.chooseMarker=#"Art";
}
else if ([assistantGroup.thirdLevel intValue]==3) {
self.chooseMarker=#"Religious";
}
else if ([assistantGroup.thirdLevel intValue]==4) {
self.chooseMarker=#"Monument";
}
else if ([assistantGroup.thirdLevel intValue]==5) {
self.chooseMarker=#"Natural";
}
else if ([assistantGroup.thirdLevel intValue]==6) {
self.chooseMarker=#"Beach";
}
NSLog(#"************ %# %# %#",assistantGroup.title,assistantGroup.latitude,assistantGroup.longitude);
Annotation *ann = [[Annotation alloc] initWithLong:[assistantGroup.longitude doubleValue] Lat:[assistantGroup.latitude doubleValue] iconNumber:0];
ann.title = assistantGroup.title;
ann.subtitle=#"";
ann.type=self.chooseMarker;
[self.annotations addObject:ann];
}
//ann.type=assistantGroup.kind;
}
[self.mapView addAnnotations:self.annotations];
}
.....
}
You should consider using a modal view controller instead since using lots of buttons in an AlertView can be confusing. I don't see the reason adding lots of buttons to a single alert view. A modal VC has all the flexibility you are looking for. Otherwise an action sheet would be preferable too.
But to answer the question: Add a "More" button last that spawns a second AlertView with the buttons that don't fit the first alert view. Alert VCs don't rescale the way they do on iOS versions above 7.0.
This question already has answers here:
How to use an UIActionSheet within an if statement? [closed]
(2 answers)
Closed 9 years ago.
I have an if else if statement that is giving me trouble.
First, here's my code:
if([_hasUserTakenAPhoto isEqual: #"YES"]) {
_xButtonAfterPhotoTaken = [[UIActionSheet alloc] initWithTitle:#"Delete" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
[_xButtonAfterPhotoTaken showInView:self.view];
_xButtonActionSheetTitle = [_xButtonAfterPhotoTaken buttonTitleAtIndex:0];
} else if ([_xButtonActionSheetTitle isEqualToString:#"Delete"]) {
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
} else {
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
My problem is that I need to perform the method call of:
[_xButtonActionSheetTitle isEqualToString:#"Delete"] and if it is equal to "Delete", then I need to perform a segue using this method call:
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
The only way I know how to do this is with another if statement, which you can see in my original code I started with "else if", but this creates different scope for all of my action sheet statements.
I just need to make the title of "Delete" on my action sheet tappable and it needs to trigger the segue.
I was trying to follow this action sheet answer here: https://stackoverflow.com/a/17374248/3117509
I just can't seem to figure this out.
Closing question. The problem is that I do not have a solid grasp on UIActionSheets.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I get this alert code when I press a button it opens a message:
- (void)patchButtonPressed
{
UIAlertView *patchAlert = [[UIAlertView alloc] initWithTitle:#"" message:#"After clicking `OK` you will be redirected to Cydia" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[patchAlert show];
[patchAlert release];
But I want that after clicking OK, it redirects to an URL (cydia://package/mypackage).
How can I do this?
set the delegate of the alertView and in the didDismiss method open the url
UIAlertView *patchAlert = [[UIAlertView alloc] initWithTitle:#"" message:#"After clicking `OK` you will be redirected to Cydia" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
pathAlert.delegate = self;
[patchAlert show];
[patchAlert release];
...
- alertView:(id)alert didDismissWithButton:(int)index {
[[UIpplication sharedApplication] openURL:[NSURL URLWithString:#"BLA"];
}
You don't actually need pathAlert.delegate = self. You've already set the delegate in the initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles: method call.
In your .h file, you need to do this:
#interface YourViewControllerName : UIViewController <UIAlertViewDelegate>
And in the .m file add this method:
- alertView:(id)alert didDismissWithButton:(int)index {
[[UIpplication sharedApplication] openURL:[NSURL URLWithString:#"foo"];
}
Alternatively, and possibly better might be to give the user the option of "Ok" to be redirected or "Cancel" to not navigate to that page. In which case, you need to create the alert as such:
UIAlertView *patchAlert = [[UIAlertView alloc] initWithTitle:#""
message:#"After clicking `OK` you will be redirected to Cydia"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
Then, you can modify the handle method as such:
- alertView:(id)alert didDismissWithButton:(int)index {
if(index == 1) {
[[UIpplication sharedApplication] openURL:[NSURL URLWithString:#"foo"];
}
}
Now the URL is only opened if they click "OK", otherwise nothing happens.
I want to make a "terms and conditions" screen that pops up the very first time that app is opened. On this view I would add a button that says "agree," and upon clicking it the code would execute:
[self dismissViewControllerAnimated:YES completion:nil];
...and would go to the first view of the app.
I am currently using a Tab Bar Controller that has 4 ViewControllers. So basically, I just need to have some method in viewWillAppear on my first ViewController that checks for an NSUserDefault key:value. The first time the app opens, it will be zero. After they click agree, I'll set it to 1 and the bit of code would never execute again.
Can you please offer some code to accomplish the task of routing the view from the firstViewController's view to this alternate view controller upon loading the app?
Thanks!
In the viewWillAppear method in your FirstViewController, check NSUserDefaults then present your TermsViewController. After user click agree in TermsViewController, set NSUserDefaults then call
[self.presentingViewController dismissModalViewControllerAnimated:YES]
The use of the popover window can get complicated. Try something like the following if you have little experience with Objective-C.
- (void)viewDidLoad {
if ([termsvalue == 0]) {
NSString *msg = [NSString stringWithFormat:#"Do you agree with the terms of use?"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"- Confirmation -"
message:msg
delegate:self
cancelButtonTitle:#"Disagree"
otherButtonTitles:#"I agree", nil];
[alert setTag:100];
[alert show];
}
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { // Validation
if ([alertView tag] == 100) {
return YES;
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { // Go
if ([alertView tag] == 100) {
if (buttonIndex == 1) {
// The user has agreed
}
}
}