Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
警报视图不会消失[iOS]_Ios_Iphone_Objective C - Fatal编程技术网

警报视图不会消失[iOS]

警报视图不会消失[iOS],ios,iphone,objective-c,Ios,Iphone,Objective C,我创建了一个自定义警报视图,其中创建了自定义按钮并删除了默认按钮。单击自定义按钮时,警报视图不会消失。请帮我解决这个问题。我正在使用以下代码 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; [alertView show]; UILabel *lbladdblock=[[UI

我创建了一个自定义警报视图,其中创建了自定义按钮并删除了默认按钮。单击自定义按钮时,警报视图不会消失。请帮我解决这个问题。我正在使用以下代码

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alertView show];
UILabel *lbladdblock=[[UILabel alloc] initWithFrame:CGRectMake(100,25,100,30)];
[alertView addSubview:lbladdblockname];

1.包括您的.h文件:UIAlertViewDelegate

2.请遵循以下实施方式

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; 
[alertView show];
UILabel *lbladdblock=[[UILabel alloc] initWithFrame:CGRectMake(100,25,100,30)];
[alertView addSubview:lbladdblockname];    
[self performSelector:@selector(dismiss:) withObject:alert1 afterDelay:1.0];
排除方法将是

-(void)dismiss:(UIAlertView*)alert
{
    [alert dismissWithClickedButtonIndex:0 animated:YES];
}
或者您希望自定义视图而不是使用

 UIAlertView*testAlert=[[UIAlertView alloc] initWithFrame:CGRectMake(0, 0, 140, 140)];


UIButton*testButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[testButton setFrame:CGRectMake(20, 20, 40, 40)];

[testButton setTitle:@"Hello" forState:UIControlStateNormal];
[testButton addTarget:self action:@selector(someAction) forControlEvents:UIControlEventTouchUpInside];
[testAlert addSubview:testButton];
 [testAlert show];

-(void)someAction

{
[testAlert removeFromSuperView];
}

如果您的类是UIAlertView的子类,则可以通过调用此方法来解除它

[alert dismissWithClickedButtonIndex:0 animated:TRUE];

如果您在课堂内使用此方法,可以使用self代替alert

要解决此问题,请执行以下操作:

在标题中定义
alertView
,然后:

- (void)viewDidLoad 
{
    alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];

    UILabel *lbladdblock=[[UILabel alloc] initWithFrame:CGRectMake(0,0,20,30)];
    [lbladdblock setText:@"custom Message"];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setTitle:@"Dismiss!" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(dismissMe) forControlEvents:UIControlEventTouchUpInside];

    [alertView addSubview:lbladdblock];
    [alertView addSubview:btn];

    [alertView show];
}

 -(void)dismissMe
{
    [alertview dismissWithClickedButtonIndex:0 animated:YES];
}
另外,在ios7中,AFAIK
addSubView
alertView
是不可能的,因此您的代码只适用于以前的版本。看这个

由于您没有使用UIAlertView的内置功能(在所有参数中传递nil),因此您应该更喜欢一些自定义警报视图。请参见链接中的示例警报视图


希望有帮助

在此处共享您的代码。UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:nil消息:nil委托:自取消按钮:nil其他按钮:nil];[警报视图显示];UILabel*lbladdblock=[[UILabel alloc]initWithFrame:CGRectMake(100,25100,30)];[alertView addSubview:lbladdblockname];点击按钮时显示代码。您在何处声明和使用自定义按钮?-(无效)GoTowelcomesScreen{//profileInfoViewController*配置文件=[self.storyboard InstanceDeviceController WithiIdentifier:@“profileInfo”];scrollingViewController*配置文件=[self.storyboard InstanceDeviceController WithiIdentifier:@“scroller”];profile.userImagePath=regUserPhoto;profile.phonenum=regPhNumber;profile.selfregistrationId=[NSString stringWithFormat:@“%d”,registeredid];[self-presentModalViewController:配置文件动画:是];}我正在使用此代码,但延迟后警报视图将显示。它不会等到我单击标签Hey i edit my answer check希望它会有所帮助谢谢mokujin。你的第一种方法对我很有效:)