IBAction within UIAlertView xcode 4.3

被刻印的时光 ゝ 提交于 2019-12-12 02:53:28

问题


I have a counter in my app that has a reset button, but instead of resetting immediately after tapped, I want a UIAlertView to popup and have the user tap Reset again as a button in the alert. I'm not entirely well experienced in this so I would ask that your answer just simply add/replace parts of my code below. I'm working on this lol. Thanks for any help!

- (IBAction)reset {
    UIAlertView *alert = [[UIAlertView alloc] 

                          initWithTitle:@"titleHere"
                          message:@"messageHere"
                          delegate: self
                          cancelButtonTitle:@"Cancel"
                          otherButtonTitles:@"Reset", nil];
    alert.tag = TAG_RESET;
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == TAG_DEV) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite.com"]]; // just another alert don't worry about this one
    } else if (alertView.tag == TAG_RESET) { // i need help here        
    }
}

So basically just an IBAction within an alert.

UPDATE: How would I incorporate this to the button?

-(IBAction)zero {
    counter=0;
    count.text = [NSString stringWithFormat:@"%i",counter];
}

UPDATE2:

So I did this, which now clears the count perfectly, the only issue is now the alert continues to pop up after you tap either cancel or reset...

-(IBAction)resetButtonPushed {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"titleHere"
                                                    message:@"messageHere"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Reset", nil];
    alert.tag = TAG_RESET;
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == TAG_DEV){
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite.com"]]; // just another alert don't worry about this one
    }
    else if (alertView.tag == TAG_RESET) { [self resetButtonPushed]; {
        counter=0;
        count.text = [NSString stringWithFormat:@"%i",counter];
    }        if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"Reset"]){
        // Reset button tapped
        }
    }
}

回答1:


I would advise against marking your zero function as IBAction when you're looking over your code later it will be confusing.

The naming of your reset method could also be confusing. I would suggest a more descriptive name. And also use of the standard IBAction method signature of taking in a sender parameter. Like so:

-(IBAction)resetButtonPushed:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"titleHere"
                                                    message:@"messageHere"
                                                   delegate: self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Reset", nil];
    alert.tag = TAG_RESET;
    [alert show];
}

It would seem you are familiar with the UIAlertViewDelegate paradigm of waiting for a callback. Since you are using -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex you would use the clickedButtonAtIndex:(NSInteger)buttonIndex parameter passed into the delegate method. Like so:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == TAG_DEV){
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite.com"]]; // just another alert don't worry about this one
    }
    else if (alertView.tag == TAG_RESET) {
        if (buttonIndex == alertView.cancelButtonIndex){
            // reset alert cancel button was tapped
        }
        else {
            // Reset button tapped
        }
    }
}

Alternatively you could do something like:

if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"Reset"]){
    // Reset button tapped
}

This section in response to comment

I'm not sure why you're having difficulty understanding how to call your zero method. I assume it's because the term IBAction is unnecessarily distracting you. IBAction is another way of saying void. The only difference is that when you use IBAction interface builder "sees" the method. As far as code is concerned the following two method definitions are identical.

-(void)zero;
-(IBAction)zero;

Again I would also advise against the IBAction and for a more descriptive method name. Perhaps something like:

-(void)zeroTheCounter {
    counter=0;
    count.text = [NSString stringWithFormat:@"%i",counter];
}

You would of course call this method just like any other, to use it in the context from the above example.

        else {
            // Reset button tapped
            [self zeroTheCounter];
        }

Response to "edit 2" Of course it shows another alert view, you ask it to with the [self resetButtonPushed] call in the alertview delegate method.

This edit uses the code currently in you question.

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == TAG_DEV){
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite.com"]]; // just another alert don't worry about this one
    }
    else if (alertView.tag == TAG_RESET) { 
        //[self resetButtonPushed]; { calling this will cause another alertview.
        //counter=0; this is done is zero
        //count.text = [NSString stringWithFormat:@"%i",counter]; this is done in zero
        //} 
        if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"Reset"]){
            // Reset button tapped
            [self zero];
        }
    }
}


来源:https://stackoverflow.com/questions/9592942/ibaction-within-uialertview-xcode-4-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!