Ios 通过视图控制器中的alertview按钮显示按钮
我有一个alertview,它在每次启动应用程序时都会出现。当我单击alertview的“显示”按钮时,我想在viewcontroller上显示一个按钮当我单击“取消”按钮时,此按钮将不显示。我正在使用此代码来执行此操作Ios 通过视图控制器中的alertview按钮显示按钮,ios,objective-c,Ios,Objective C,我有一个alertview,它在每次启动应用程序时都会出现。当我单击alertview的“显示”按钮时,我想在viewcontroller上显示一个按钮当我单击“取消”按钮时,此按钮将不显示。我正在使用此代码来执行此操作 - (void )alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { ViewController *cont
- (void )alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
ViewController *controller = [[ViewController alloc]init];
controller.button.hidden= NO;
}
在viewcontroller中,我创建了按钮的出口。并在视图中完成了以下代码,加载了视图控制器,但我无法显示
钮扣
仅在
viewDidLoad
self.button.hidden = NO;
您必须替换ViewDod load中的代码…
UIAlertView
已弃用。将UIAlertController
与首选样式UIAlertControllerStyleAlert
一起使用
您当前的代码可以如下所示:
代表,m
ViewController *controller = [[ViewController alloc]init];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//Show button
controller.button.hidden = NO;
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
//Hide button
controller.button.hidden = YES;
}];
[alert addAction:ok];
[alert addAction:cancel];
[window.rootViewController presentViewController:alert animated:YES completion:nil];
viewcontroller.m
- (void)viewDidLoad
{
[super viewDidLoad];
//self.button.hidden = NO; remove this line
}
试着改变
ViewController *controller = [[ViewController alloc]init];
到
检查它是否工作 创建UIAlertView时,将ViewController(与您的rootViewController相同的实例)设置为其委托,然后在ViewController中实现
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
委托方法。在那里,您可以使用self.button同一视图中的两个操作控制器在视图中不允许alloc init。@Ashish Kakkad为什么不允许?@Tim007如果您在当前视图中,那么需要alloc吗?@Ashish Kakkad啊,我明白了,允许init隐藏按钮lol:D
ViewController *controller = [[ViewController alloc]init];
ViewController *controller = [[ViewController alloc]initWithNibName:nibName];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex