2016-03-07 132 views
3

我有一个alertview它每次启动应用程序时出现。当我单击取消按钮然后单击按钮显示按钮时,我想要在viewcontroller上显示一个按钮,则此按钮不会显示。我正在使用此代码来执行此操作。通过视图控制器中的alertview按钮显示一个按钮

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

    ViewController *controller = [[ViewController alloc]init]; 
    controller.button.hidden= NO; 
} 

和视图 - 控制我创建按钮的出口。并做了下面的代码视图做视图控制器的负荷,但我无法显示 按钮

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

self.button.hidden = YES; 
} 
+0

这两个动作在同一个视图控制器中 –

+0

不要分配init视图。 –

+0

@Ashish Kakkad为什么不呢? –

回答

0

UIAlertView已弃用。改为使用UIAlertController而不是UIAlertControllerStyleAlert的preferredStyle。

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    //Init hide button 
    self.button.hidden = YES; 
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:nil preferredStyle:UIAlertControllerStyleAlert]; 
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
     //Show button 
     self.button.hidden = NO; 
    }]; 
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { 
     //Hide button 
     self.button.hidden = YES; 
    }]; 
    [alert addAction:ok]; 
    [alert addAction:cancel]; 
    [self presentViewController:alert animated:YES completion:nil]; 
} 

您当前的代码可以是这样的:

delegate.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 
} 
1

只在viewDidLoad

self.button.hidden = NO; 

加入这行,你必须更换viewdid负载代码...

1

尝试改变

ViewController *controller = [[ViewController alloc]init]; 

TO

ViewController *controller = [[ViewController alloc]initWithNibName:nibName]; 

检查它是否有效!

1

当您创建的UIAlertView中设置视图控制器(这将是你RootViewController的同一个实例),以它的委托,然后在视图控制器实现

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 

委托方法。在那里你可以使用self.button

相关问题