Reputation: 15
first of all I want to thank all of you! :)
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];
}];
}
}
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=====
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];
}
Upvotes: 0
Views: 1584
Reputation: 11039
- (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];
}
}
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
Then you must present your new vc on VCx, i.e. on the last vc that currently presented/visible.
Upvotes: 1
Reputation: 2417
My guess based on the description (and if I understand your code correctly) is your dismissing the mainViewController and then after it's gone your it to present the view controller. Since it's been dismissed it's not allowed to present anything.
It's like when the girl scouts come to your house to deliver your cookies you tell them "Go back home and don't come back, and when you get home leave my cookies on my front door."
Try moving the code that dismisses the mainViewController and then presents the new controller into the view controller that originally presented mainViewController.
Edit:
I found another error in your UITableviewCell class. The problem is this line, located in your panGestureRecognizer function.
TableViewController *mainController = [[TableViewController alloc] init];
Your creating an instance of mainController and then a few lines later your calling
[mainController sendSMS];
Which is trying to present a view controller, however that instance of mainController was never pushed onto the view stack, thus the error.
What you need to do is call sendSMS on on the tableviewController instance which is the one that holds the tableview, and thus the tableViewCell. Heres another post which talks about how to do that Reference from UITableViewCell to UITableView to UINavigationController
Upvotes: 1