簡體   English   中英

如何在 ios 中關閉視圖控制器?

[英]How to dismiss viewcontroller in ios?

我創建了一個庫,如果主應用程序調用我的庫,它會顯示它,並從服務器下載一些數據。 但是如果服務器有一些錯誤,我想終止庫視圖,但它不起作用我在主機應用程序中有一個委托:

-(void)libraryResult:(NSString*)result{
NSLog(@"result: %@", result);
}

我在 viewWillAppear 方法中從服務器下載數據,下載有一個這樣的委托方法:

-(void)networkManagerError:(NSString *)error{
[hud hide:YES];
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
[self.delegate libraryResult:error];
}

我在日志中看到,應用程序返回到主應用程序,但視圖沒有改變。 如何解決這個問題? 我的代碼有什么問題?

改變這一行

[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];

[self dismissViewControllerAnimated:YES completion:nil];

試試這些..

[self dismissViewControllerAnimated:YES completion:nil];

如果您有 VC1 呈現 VC2,那么在 VC2 內部的相應事件(比如關閉按鈕點擊,或服務器錯誤等),您應該調用:

[self.presentingViewController dismissViewControllerAnimated:YES completion:^{

    }]

如果您以這樣一種方式設置事物,即 VC1 會收到有關 VC2 內部發生的事件的通知,您可以使用:

[self dismissViewControllerAnimated:YES completion:nil];

然而,第一種方法更受歡迎,因為它是一種更好的設計實踐,並且有助於 VC1 和 VC2 之間更松散的耦合。

如果發生錯誤,請嘗試從“庫結果”方法發出通知,並將觀察者添加到當前控制器視圖中。

[[NSNotificationCenter defaultCenter] postNotificationName:Remove_CurrentView object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(remove) name:Remove_CurrentView object:nil];

-(void)remove{
    [self dismissViewControllerAnimated:YES completion:nil];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM