簡體   English   中英

以編程方式執行操作時,UIViewController顯示為黑屏

[英]UIViewController appears as Black screen when doing programmatically

以編程方式處理時,UIViewController顯示為黑屏

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *user = (NSString *) [self.friends objectAtIndex:indexPath.row];
ChatViewController *chatController = [[ChatViewController alloc] initWithUser:user];
[self presentModalViewController:chatController animated:YES];

}

下面給出的代碼在chatviewcontroller中

 - (id) initWithUser:(NSString *) userName {
if (self = [super init]) {
    chatWithUser = userName;   
}
return self;
}

當我使用情節提要segue進行操作時,僅選擇了tableview行,但未顯示ChatViewController

else if ([segue.identifier isEqualToString:@"showChatView"]) {
    ChatViewController *viewController  = (ChatViewController *)segue.destinationViewController;
      viewController.chatWithUser = friends;
}

如果有人能弄清楚我在做什么錯。 會非常感激。

謝謝您的幫助。

presentModalViewController:animated:已棄用(自iOS 6起),您應該使用presentViewController:animated:completion:

但是,您似乎正在使用segue來訪問ChatViewController,因此您甚至不必展示視圖控制器,因為這是由Interface Builder處理的。 如果您的腳本設置正確,請用[self performSegueWithIdentifier:@"showChatView" sender:nil];替換presentModalViewController:animated: [self performSegueWithIdentifier:@"showChatView" sender:nil];

編輯您應該將ChatViewController設置移至prepareForSegue:sender:方法,如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *user = (NSString *)[self.friends objectAtIndex:indexPath.row];
    [self performSegueWithIdentifier:@"showChatView" sender:user];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showChatView"]) {
        NSString *user = (NSString *)sender;
        ChatViewController *chatVC = (ChatViewController *)[segue destinationViewController];
        // No need to have an init method with the user property since Interface Builder does that for you.
        chatVC.chatWithUser = user;  // Expose this property in ChatViewController's header file if it's not already
}

那應該是您在代碼中需要做的所有事情。

呈現與顯示視圖控制器

UIViewController類提供了兩種顯示視圖控制器的方式:

  • showViewController:sender:showDetailViewController:sender:方法提供了顯示視圖控制器的最靈活,最靈活的方法。 這些方法使演示視圖控制器可以決定如何最好地處理演示。 例如,容器視圖控制器可以將視圖控制器作為子級合並,而不是模態地呈現。 默認行為以模態形式顯示視圖控制器。

  • presentViewController:animated:completion:方法始終以模態顯示視圖控制器。 調用此方法的視圖控制器可能最終不會處理演示文稿,但是演示文稿始終是模態的。 此方法使顯示樣式適合水平緊湊的環境。

showViewController:sender:showDetailViewController:sender:方法是啟動演示文稿的首選方法。 視圖控制器可以調用它們,而無需了解其余的視圖控制器層次結構或當前視圖控制器在該層次結構中的位置。 這些方法還使您更容易在應用的不同部分中重用視圖控制器,而無需編寫條件代碼路徑。

請參考此鏈接,以了解編程編程和使用界面生成器之間的區別。 希望對您有所幫助。

@timgcarlson的答案非常適合解決您的問題。

根據@Sneha的建議,我添加了一段很有用的段落。

暫無
暫無

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

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