警告:尝试显示*其视图不在窗口层次结构中的对象 [英] Warning: Attempt to present * whose view is not in the window hierarchy

查看:48
本文介绍了警告:尝试显示*其视图不在窗口层次结构中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我要感谢大家! :)

first of all I want to thank all of you! :)

所以,我有一个带Table View的主视图控制器,其中包含来自另一个类的自定义单元格。我对单元格进行了自定义选择(您必须滑动视图的单元格)。在该操作中,我正在主View Controller中调用方法来发送SMS。但是我收到此警告,并且没有显示SMS控制器。我知道,此错误消息表示无法打开SMS控制器,因为未加载主控制器?我从这里尝试了一些代码,但是对我没有用。我的班级目前看起来像这样:

So, I have a Main view Controller with Table View, with custom cells from another class. I have made custom selection of the cell (you have to slide the cell of the view). In that action I'm calling method in the main View Controller to send SMS. But I'm getting this warning, and the SMS controller is not appearing. I know, that this error message means, that it can't open SMS Controller, because the main Controller is not loaded? I tried some codes from here, but nothing works for me. My classes currently look like this:

Main controller.m

.
.
.

-(void)viewWillAppear:(BOOL)animated
{
    [self.citiesButton setTitle:rowValue forState:UIControlStateNormal];

    UITableView *tableView = (id)[self.view viewWithTag:1];
{
    tableView.rowHeight = 100;
    UINib *nib  = [UINib nibWithNibName:@"TableViewCell" bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:CellTableIdentifier];

    UIEdgeInsets contentInset = tableView.contentInset;
    contentInset.top = 20;
    [tableView setContentInset:contentInset];

    NSLog(@"rowValue is %@", rowValue);
    self.city = [_dict objectForKey:rowValue];
}

    NSLog(@"View will appear");
    [tableView reloadData];
}

-(void)sendSMS
{
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
    if([MFMessageComposeViewController canSendText])

    {
        controller.body = @"Testy Test";
        controller.recipients = [NSArray arrayWithObjects:@"774252704", nil];
        controller.messageComposeDelegate = self;

        [self dismissViewControllerAnimated:YES completion:^{
        [self presentViewController:controller animated:YES completion:nil];
    }];

}
}

所以我要关闭首先是主控制器,然后显示smsController,但是仍然没有运气。我认为问题出在我从TableViewCell类调用该方法,但是仍然是,表是在视图加载后创建的,所以我真的在这里迷路了。

So I'm trying to close the main controller first and then display the smsController, but still no luck. I think, that the problem will be in that I'm calling the method from the TableViewCell class, but still, the table is created after the view is loaded, so I'm really lost here.

非常感谢您的宝贵时间!

Thank you very much for your time!

===== EDIT =====

=====EDIT=====

这是自定义单元格类,在这里我称为sendSMS方法

Here is the custom cell class, from where I call the sendSMS method

tableViewCell.m

#import "TableViewCell.h"
#import "TableViewController.h"

static NSString *CellTableIdentifier = @"TableViewCell";

@implementation TableViewCell

@synthesize timeLabel = _timeLabel;
@synthesize priceLabel = _priceLabel;
@synthesize infoLabel = _infoLabel;


- (void)awakeFromNib
{
    // Initialization code
}


-(void)layoutSubviews
{
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizer:)];

    [self addGestureRecognizer:panGesture];
}


-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:CellTableIdentifier];



    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)panGestureRecognizer:(UIPanGestureRecognizer *)sender{
    CGPoint translation = [sender translationInView:self];

    TableViewController *mainController = [[TableViewController alloc] init];

    //NSLog(@"Panned with translation point: %@", NSStringFromCGPoint(translation));

    sender.view.center = CGPointMake(sender.view.center.x + translation.x,
                                 sender.view.center.y);


    CGPoint breakingPoint = CGPointMake(320,sender.view.center.y);
    CGPoint startPoint = CGPointMake(150, sender.view.center.y);
    CGPoint endPoint = CGPointMake(500, sender.view.center.y);

    if (sender.view.center.x <= startPoint.x) {
        sender.view.center = startPoint;
    }

    if (sender.state == UIGestureRecognizerStateEnded) {
        if (sender.view.center.x >= breakingPoint.x) {

            [UIView animateWithDuration:0.2
                              delay:0.0
                            options: UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             sender.view.center = endPoint;
                         }
                         completion:^(BOOL finished){
                             NSLog(@"Bought!");
                             [mainController sendSMS];
                         }];

    } else {
        //recognizer.view.center = startPoint;

        [UIView animateWithDuration:0.2
                              delay:0.0
                            options: UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             sender.view.center = startPoint;
                         }
                         completion:^(BOOL finished){
                             NSLog(@"Returned!");
                         }];
    }
}
    [sender setTranslation: CGPointZero inView: self];
}


推荐答案

- (void)presentViewController:(UIViewController *)viewController animated:(BOOL)animated onComplete:(void (^)(void))callback
{
    AppDelegate *APP_DELEGATE = [UIApplication sharedApplication].delegate;

    UIViewController *presentedModalVC = [APP_DELEGATE.window.rootViewController presentedViewController];

    if (presentedModalVC) {
        while (presentedModalVC.presentedViewController) {
            presentedModalVC = presentedModalVC.presentedViewController;
        }
        [presentedModalVC presentViewController:viewController animated:animated completion:callback];
    } else {
        [APP_DELEGATE.window.rootViewController presentViewController:viewController animated:animated completion:callback];
    }
}

-(void)sendSMS
{
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
    if([MFMessageComposeViewController canSendText])

    {
        controller.body = @"Testy Test";
        controller.recipients = [NSArray arrayWithObjects:@"774252704", nil];
        controller.messageComposeDelegate = self;

        [self presentViewController:controller animated:YES onComplete:nil];
    }              
}

如果当前显示的vc,则必须显示vc存在,即如果您具有呈现的vc的这种层次结构

You must present your vc on currently presented vc, if that exist, i.e. if you have this hierarchy of presented vc's


VC1---  
  VC2 - presented by VC1-----  
    VC3 - presented by VS2----  
...  
      VCx - presented by x-1 

然后,您必须在VCx上显示新的vc,即在当前显示/可见的最后一个vc上。

Then you must present your new vc on VCx, i.e. on the last vc that currently presented/visible.

这篇关于警告:尝试显示*其视图不在窗口层次结构中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆