【问题标题】:show alert view after certain score一定分数后显示警报视图
【发布时间】:2012-07-06 14:04:25
【问题描述】:

我正在 Xcode 上创建一个应用程序,我希望在他们单击按钮 100 次时显示警报视图,但由于某种原因,当我单击按钮 100 次时没有任何反应,我将向您显示我拥有的所有代码目前在应用程序中以及我在 xib 中所做的事情以及与希望你们提供帮助的相关内容。

#import <UIKit/UIKit.h>

int counter;

@interface ViewController : UIViewController {

    IBOutlet UILabel *count;
}

-(IBAction)plus;
-(IBAction)minus;
-(IBAction)zero;

@end

实现

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


- (void) buttonAction {
    counter++;
    if(counter == 100)
        [self showAlert];
}


- (void) showAlert {

    UIAlertView *alert = [[UIAlertView alloc]

                          initWithTitle:@"Tile"
                          message:@"This is the message" 
                          delegate:nil 
                          cancelButtonTitle:@"Dismiss" 
                          otherButtonTitles:nil];

    [alert show];

}

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

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

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




- (void)viewDidLoad {
    counter=0;
    count.text = @"0";
        [super viewDidLoad];

}




- (void)viewDidUnload {


     [super viewDidUnload];
    }





- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


@end

这就是我拥有的所有代码,我遇到的问题是计数器工作正常,就在我达到 100 时,没有任何反应,这是图像

并且没有警报

【问题讨论】:

    标签: ios xcode4 uialertview xcode4.3 alert


    【解决方案1】:

    我只是将 if 语句放在你的 plus 函数中,而不是使用“buttonAction”函数。类似这样的东西:

    -(IBAction)plus {
        counter=counter + 1;
        count.text = [NSString stringWithFormat:@"%i",counter];
        if(counter == 100)
            [self showAlert];
    }
    

    它消除了对那个额外函数的需要,如果无论如何调用它都会增加太多计数器。 (plus 和 buttonAction 都告诉 counter 增加 1,意思是每次 plus 被称为 counter 增加 2)

    【讨论】:

    • 使用这种方法我无法得到我在 101 上按减号按钮的情况
    • 确实如此。我没有意识到你会想要它作为一个选项。在这种情况下,我建议将if(counter == 100){[self showAlert];} 添加到减号函数的末尾。我相信这可能是添加所需功能的最简单方法。如果你想将它扩展到相当多的不同按钮和方法,我建议创建一个函数来检查它是否为 100,但是对于两个按钮来说,全新的方法似乎有点过头了。
    • @ChanceR。你好,我正在尝试添加一个按钮,但它的进展并不顺利,重点会说“评价这个应用程序”你能帮助哥们吗?
    • 以下是我创建按钮的步骤
    • 1.添加按钮以在界面生成器中查看,将文本更改为“评价此应用程序” 2.在视图控制器中实例化它@interface IBOutlet UIButton *mybutton; 3.在@interface @之后为其创建一个属性987654324@ 4.创建一个方法来处理被按下的按钮-(void)buttonPressed; 5.在.m 文件中,具体指定您希望按钮执行的操作,在这种情况下,它可能会转到应用商店。我帮不了你,但谷歌有答案。 5.link接口生成器中的方法。
    【解决方案2】:

    你没有调用你的“buttonAction”方法。试试这个:

    -(IBAction)plus {
        counter=counter + 1;
        [self buttonAction];
        count.text = [NSString stringWithFormat:@"%i",counter];
    }
    
    -(IBAction)minus {
        counter=counter - 1;
        [self buttonAction];
        count.text = [NSString stringWithFormat:@"%i",counter];
    }
    

    【讨论】:

    • 是的,谢谢你的工作,但由于某种原因,它现在一分为二?
    • 它将计数为 2,因为您在 Plus 方法和 Button Action 方法中都增加了计数器 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多