2012-10-26 80 views
5

這是我的應用程序設計。我有mainController其中介紹secondViewController。現在,我想解僱secondViewController,隨後調用該方法aMethodmainController像這樣:dismissViewControllerAnimated完成方法不起作用

[self dismissViewControllerAnimated:YES completion:aMethod]; 

但是,這給我的錯誤use of undeclared identifier 'aMethod'

顯然我沒有正確地使用完成處理,但我想不通走出正確的道路。

回答

12

我覺得這是你在找什麼,

[self dismissViewControllerAnimated:YES completion:^{ 
      [self.mainController aMethod]; 
     }]; 

在你需要聲明self外塊,並把它作爲,

上面的代碼
__block SecondViewController *object = self; 

[self dismissViewControllerAnimated:YES completion:^{ 
       [object.mainController aMethod]; 
      }]; 

只是爲了避免self被攔截。

更新:

現在得到的問題。您需要聲明mainController作爲您的.h文件secondViewController中的一個屬性。之後,當你呈現從maincontrollersecondViewController,你需要將其設置爲,

secondViewController.maincontroller = self; 
[self presentViewController:secondViewController animated:YES completion:Nil]; 

在你SecondViewController.h文件,

@property(nonatomic, assign) MainController *mainController; 

在你SecondViewController.m文件,

@synthesis mainController; 

更新2:

如果您不想聲明maincontroller作爲屬性,請嘗試此操作。我不確定這是否是正確的做法。但它看起來像過去一樣工作。

MainController *mainController = (MainController *)[self.view.superview nextResponder]; 

    [self dismissViewControllerAnimated:YES completion:^{ 
        [mainController aMethod]; 
       }]; 

更新3(建議):

這應該爲你工作。覈實。

MainController *mainController = (MainController *)self.parentViewController; 

    [self dismissViewControllerAnimated:YES completion:^{ 
        [mainController aMethod]; 
       }]; 
+0

以及ig我在做'__block YouViewController * object = self' wouldnt我將設置對象是viewController被解僱?不是正在呈現的人? –

+0

噢,剛剛看到您的更新。對象沒有mainController,它不知道它是什麼.. –

+0

你現在可以檢查,讓我知道這是否工作? – iDev

-3

這樣聲明:dismissViewControllerAnimated:YES completion:Nil

我希望它能幫助。

+0

不,這沒有幫助。我試圖調用viewController的方法,一旦當前一個被解僱就會顯示...因此我使用完成 –

1

你想要的東西,如:

[self dismissViewControllerAnimated:YES completion:^{ 
      ... 
      <do something on completion here> 
      ... 
     }]; 
+0

同樣的錯誤...哪個控制器接收完成方法? –

+0

什麼是方法? – EarlyRiser

+0

它是一個在'mainController'中聲明的方法,它是在調用dismissviewcontrolcontrolleranimated –

相關問題