UIAlertView xcode 4.3 中的 IBAction [英] IBAction within UIAlertView xcode 4.3

查看:28
本文介绍了UIAlertView xcode 4.3 中的 IBAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个计数器,它有一个重置按钮,但不是在点击后立即重置,而是希望弹出一个 UIAlertView 并让用户再次点击重置作为警报中的按钮.我在这方面并不完全有经验,所以我会问你的答案只是简单地添加/替换我下面的部分代码.我正在研究这个哈哈.感谢您的帮助!

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

所以基本上只是警报中的 IBAction.

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

更新2:

所以我这样做了,现在可以完美地清除计数,唯一的问题是现在在您点击取消或重置后警报继续弹出......

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

推荐答案

我建议不要在以后查看代码时将 zero 函数标记为 IBAction会很混乱.

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

重置方法的命名也可能令人困惑.我会建议一个更具描述性的名称.并且还使用标准的 IBAction 方法签名来接收发送者参数.像这样:

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

您似乎熟悉等待回调的 UIAlertViewDelegate 范例.由于您使用的是 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex,您将使用传递给委托的 clickedButtonAtIndex:(NSInteger)buttonIndex 参数方法.像这样:

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
}

本节回复评论

我不知道为什么您在理解如何调用 zero 方法时遇到困难.我认为这是因为术语 IBAction 不必要地分散了您的注意力.IBActionvoid 的另一种说法.唯一的区别是当您使用 IBAction 接口构建器看到"该方法时.就代码而言,以下两个方法定义是相同的.

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;

我还建议不要使用 IBAction 并建议使用更具描述性的方法名称.也许是这样的:

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

对edit 2"的响应 当然,它显示了另一个警报视图,您可以通过 alertview 委托方法中的 [self resetButtonPushed] 调用来请求它.

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

这篇关于UIAlertView xcode 4.3 中的 IBAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆