2016-03-07 97 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

相關問題