簡體   English   中英

從另一個UIViewController在先前的UIViewController上創建UIView

[英]Create UIView on previous UIViewController from another UIViewController

彈出當前UIViewController之后,如何在以前的UIViewController上創建UIView。

在我以前的UIViewController上,我有一個可以在其他UIViewController上訪問的函數:

-(void)setMovieCompare:(NSString *)_movieName {
    NSLog(@"Movie Compare %@",_movieName);
    UIView *vw2ndPlayer = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 166)];
    [self.view addSubView:vw2ndPlayer];
}

然后在我當前的UIViewController上,我有:

VideoPlayerViewController *_VideoPlayerViewController = [[VideoPlayerViewController alloc] initWithNibName:nil bundle:nil];
[_VideoPlayerViewController setMovieCompare:mediaObject.name];
[self.navigationController popViewControllerAnimated:YES];

這導致我:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString appendString:]: nil argument'
*** First throw call stack:
(0x31a3e2a3 0x398a297f 0x31a3e1c5 0x31a0d773 0x72461 0x74077 0x33865595 0x7600b 0x7aa6d 0x3390828d 0x3398af81 0x3234c277 0x31a135df 0x31a13291 0x31a11f01 0x31984ebd 0x31984d49 0x3553b2eb 0x3389a301 0x6e89d 0x39cd9b20)
libc++abi.dylib: terminate called throwing an exception

該代碼崩潰是因為它試圖從nibNamed:nil讀取vc。 即使您能夠正常工作,也無法正常工作。 該代碼不會更改推動當前vc的VideoPlayerVC,它只是分配一個新的(嘗試)並更改對該新視圖的視圖,該視圖在該方法完成后立即釋放。

在導航容器中查看呈現視圖控制器的方法是訪問self.navigationController.viewControllers。

NSArray *vcs = self.navigationController.viewControllers;
VideoPlayerViewController *previousVC = vcs[MAX(0, vc.count-2)];
[previousVC setMovieCompare ...];

到達導航數組可能不是最好的選擇,尤其是其他視圖控制器可以顯示當前的vc。 另一種設計是委托模式,其中VideoPlayerVC在推送之前將自己設置為推送vs委托。

暫無
暫無

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

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