简体   繁体   English

以编程方式执行操作时,UIViewController显示为黑屏

[英]UIViewController appears as Black screen when doing programmatically

UIViewController appears as black screen when handling programmtically 以编程方式处理时,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];

}

This below given code is in the chatviewcontroller 下面给出的代码在chatviewcontroller中

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

and when i do it using storyboard segue then only tableview row gets selected but doesn't shows ChatViewController 当我使用情节提要segue进行操作时,仅选择了tableview行,但未显示ChatViewController

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

If anyone can figure out what im doing wrong. 如果有人能弄清楚我在做什么错。 Will appreciate so much. 会非常感激。

Thanks for help though. 谢谢您的帮助。

presentModalViewController:animated: is deprecated (since iOS 6), you should use presentViewController:animated:completion: presentModalViewController:animated:已弃用(自iOS 6起),您应该使用presentViewController:animated:completion:

However, it looks like you are using a segue to get to your ChatViewController, so you shouldn't even have to present the view controller since this is handled by Interface Builder. 但是,您似乎正在使用segue来访问ChatViewController,因此您甚至不必展示视图控制器,因为这是由Interface Builder处理的。 If your segue is set up correctly, replace presentModalViewController:animated: with [self performSegueWithIdentifier:@"showChatView" sender:nil]; 如果您的脚本设置正确,请用[self performSegueWithIdentifier:@"showChatView" sender:nil];替换presentModalViewController:animated: [self performSegueWithIdentifier:@"showChatView" sender:nil];

EDIT You should just move your ChatViewController setup to the prepareForSegue:sender: method, like so: 编辑您应该将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
}

That should be all you need to do in your code. 那应该是您在代码中需要做的所有事情。

Presenting Versus Showing a View Controller 呈现与显示视图控制器

The UIViewController class offers two ways to display a view controller: UIViewController类提供了两种显示视图控制器的方式:

  • The showViewController:sender: and showDetailViewController:sender: methods offer the most adaptive and flexible way to display view controllers. showViewController:sender:showDetailViewController:sender:方法提供了显示视图控制器的最灵活,最灵活的方法。 These methods let the presenting view controller decide how best to handle the presentation. 这些方法使演示视图控制器可以决定如何最好地处理演示。 For example, a container view controller might incorporate the view controller as a child instead of presenting it modally. 例如,容器视图控制器可以将视图控制器作为子级合并,而不是模态地呈现。 The default behavior presents the view controller modally. 默认行为以模态形式显示视图控制器。

  • The presentViewController:animated:completion: method always displays the view controller modally. presentViewController:animated:completion:方法始终以模态显示视图控制器。 The view controller that calls this method might not ultimately handle the presentation but the presentation is always modal. 调用此方法的视图控制器可能最终不会处理演示文稿,但是演示文稿始终是模态的。 This method adapts the presentation style for horizontally compact environments. 此方法使显示样式适合水平紧凑的环境。

The showViewController:sender: and showDetailViewController:sender: methods are the preferred way to initiate presentations. showViewController:sender:showDetailViewController:sender:方法是启动演示文稿的首选方法。 A view controller can call them without knowing anything about the rest of the view controller hierarchy or the current view controller's position in that hierarchy. 视图控制器可以调用它们,而无需了解其余的视图控制器层次结构或当前视图控制器在该层次结构中的位置。 These methods also make it easier to reuse view controllers in different parts of your app without writing conditional code paths. 这些方法还使您更容易在应用的不同部分中重用视图控制器,而无需编写条件代码路径。

Refer this link to know about what is the difference between segue programmatically and using interface builder. 请参考此链接,以了解编程编程和使用界面生成器之间的区别。 I hope it is helpful. 希望对您有所帮助。

@timgcarlson's answer is great for solving your problem. @timgcarlson的答案非常适合解决您的问题。

As per @Sneha's suggestion I have added a paragraph that feel is useful. 根据@Sneha的建议,我添加了一段很有用的段落。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM