简体   繁体   中英

UIViewController appears as Black screen when doing programmatically

UIViewController appears as black screen when handling programmtically

- (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

 - (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

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:

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. If your segue is set up correctly, replace presentModalViewController:animated: with [self performSegueWithIdentifier:@"showChatView" sender:nil];

EDIT You should just move your ChatViewController setup to the prepareForSegue:sender: method, like so:

- (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:

  • The showViewController:sender: and showDetailViewController:sender: methods offer the most adaptive and flexible way to display view controllers. 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. 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. 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.

As per @Sneha's suggestion I have added a paragraph that feel is useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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