Ios 在UiAlertView中按下按钮时如何运行选择器
因此,我有以下代码,当回合结束时,它会弹出以下Ios 在UiAlertView中按下按钮时如何运行选择器,ios,Ios,因此,我有以下代码,当回合结束时,它会弹出以下UIAlertView。现在,当用户按下排行榜按钮时,我希望它运行显示排行榜的-(void)语句。请不要以为这都是在一个精灵套件视图控制器。我该怎么做 另外,我在另一个类中有我的void语句,是否通过#import将其带过来 - (void) gameEnded { // indicate our game state as stopped _gameState = STOPPED; //
UIAlertView
。现在,当用户按下排行榜按钮时,我希望它运行显示排行榜的-(void)
语句。请不要以为这都是在一个精灵套件视图控制器。我该怎么做
另外,我在另一个类中有我的void语句,是否通过#import
将其带过来
- (void) gameEnded
{
// indicate our game state as stopped
_gameState = STOPPED;
// create a message to let the user know their score
NSString *message = [NSString stringWithFormat:@"You scored %d this time", _score];
// show the message to the user
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Game over!"
message:message
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:@"Leaderboards",nil];
[av show];
// reset the score tracker for the next game
_score = 0;
//reset playing area
[self removeAllBlocks];
[self addBlocks];
}
在这里:
您需要将委托设置为“self”,或者将要作为其委托处理警报视图事件的另一个类。该类必须符合UIAlertViewDelegate
,例如:
@interface YourController : YourControllerSuperClass <UIAlertViewDelegate>
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
// In case there are more then one alert view and a reference to each of the alert views are kept.
if(alertView == self.avAlertView){
switch(buttonIndex){
// Your button's cases!
}
}
}
因为这是当按下按钮时,代理上的警报视图调用的方法。
在这里,您将收到对警报视图的引用和按下按钮的索引。
通过此操作,您可以找到执行了哪些操作。
例如:
@interface YourController : YourControllerSuperClass <UIAlertViewDelegate>
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
// In case there are more then one alert view and a reference to each of the alert views are kept.
if(alertView == self.avAlertView){
switch(buttonIndex){
// Your button's cases!
}
}
}
您当前的
控制器
应实现UIAlertViewDelegate
,并使用您的排行榜
按钮的索引调用方法alertView:ClickedButtonIndex:
您的CustomController.h
:
#import <UIKit/UIKit.h>
@interface AdsEventViewController : UITabBarController<UIAlertViewDelegate>
@end
代表是坏的,区块是好的!下面是使用警报视图的更好方法。
[...]
- (void) gameEnded
{
// indicate our game state as stopped
_gameState = STOPPED;
// create a message to let the user know their score
NSString *message = [NSString stringWithFormat:@"You scored %d this time", _score];
// show the message to the user
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Game over!"
message:message
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:@"Leaderboards",nil];
[av show];
// reset the score tracker for the next game
_score = 0;
//reset playing area
[self removeAllBlocks];
[self addBlocks];
}
[...]
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == _LEADERBOARD_BUTTON_INDEX_ ){
// leaderboard method
}
}