gpt4 book ai didi

ios - 警告 : Attempt to present * whose view is not in the window hierarchy

转载 作者:行者123 更新时间:2023-11-28 22:03:57 25 4
gpt4 key购买 nike

首先要感谢大家! :)

因此,我有一个带有 TableView 的主视图 Controller ,以及来自另一个类的自定义单元格。我已经对单元格进行了自定义选择(您必须滑动 View 的单元格)。在该操作中,我调用主视图 Controller 中的方法来发送 SMS。但是我收到了这个警告,并且 SMS Controller 没有出现。我知道,这条错误消息意味着它无法打开 SMS Controller ,因为未加载主 Controller ?我从这里尝试了一些代码,但对我没有任何作用。我的类(class)目前看起来像这样:

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];
}];

}
}

所以我试图先关闭主 Controller ,然后再显示 smsController,但仍然没有成功。我认为,问题在于我从 TableViewCell 类调用该方法,但仍然在加载 View 后创建表,所以我真的迷路了。

非常感谢您的宝贵时间!

=====编辑=====

这是自定义单元格类,我从中调用 sendSMS 方法

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 的这种层次结构

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

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

关于ios - 警告 : Attempt to present * whose view is not in the window hierarchy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24457936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com