gpt4 book ai didi

ios - UIAlertView 按钮操作不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:00:44 24 4
gpt4 key购买 nike

我有一个警报 View ,当我单击"is"按钮时,它应该会产生另一个警报 View 和一条 toast 消息,但它没有发生。我想不通。这是我的代码:

-(void)myMethod {
UIAlertView *saveAlert = [[UIAlertView alloc] initWithTitle:@"First Message"
message:@"My First message"
delegate:nil
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
saveAlert.tag=0;
[saveAlert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
}

这是我用来为不同警报 View 提供功能的方法。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag==0) {

if (buttonIndex == 0)
{
//Code for Cancel button
}
if (buttonIndex == 1)
{
//code for yes button
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"Successfully displayed First Message";
hud.margin = 10.f;
hud.yOffset = 150.f;
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:3];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Second Message"
message:@"My second message"
delegate:nil
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes",nil];
alert.tag=1;
[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
}
}

if (alertView.tag==1) {

if (buttonIndex == 0)
{
//Code for Cancel button
}
if (buttonIndex == 1)
{
//Code for yes Button
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"Succesfully displayed Second Message";
hud.margin = 10.f;
hud.yOffset = 150.f;
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:3];
}
}
}

任何人都可以帮助找到问题。为什么我在第一次警报中单击"is"按钮后无法收到第二次警报?

最佳答案

您还没有为您的 UIAlertView 设置委托(delegate),并确保您的委托(delegate)符合 UIAlertViewDelegate 协议(protocol)。找到下面的代码片段。

您的 Controller 符合 UIAlertViewDelegate 协议(protocol):

@interface YourViewController : UIViewController <UIAlertViewDelegate> 

创建 UIAlertView 并设置委托(delegate):

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"First Message"
message:@"Show second message"
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[alertView show];

实现 UIAlertViewDelegate 委托(delegate)方法:

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if( 0 == buttonIndex ){ //cancel button
[alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
} else if ( 1 == buttonIndex ){
[alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
UIAlertView * secondAlertView = [[UIAlertView alloc] initWithTitle:@"Second Message"
message:@"Displaying second message"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[secondAlertView show];
}
}

关于ios - UIAlertView 按钮操作不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25298196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com