SMS视图控制器不会解雇自己

时间:2014-07-07 08:17:28

标签: ios iphone objective-c cocoa-touch

所以我有一个视图控制器,我有一个动作方法sendSMS,它创建了SMS视图控制器......问题是,当我实际发送消息时,控制器不会解雇,或者它赢了不创建任何日志,所以我猜甚至没有调用didFinishWithResult方法。非常感谢大家!

所以,我的.m看起来像这样:

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

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

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


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

    {
        NSLog(@"SMS composer appeared");

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

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

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
             didFinishWithResult:(MessageComposeResult)result
{
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MessageComposeResultCancelled:
            NSLog(@"Result: SMS sending canceled");
            break;
        case MessageComposeResultSent:
            NSLog(@"Result: SMS sent");
            break;
        case MessageComposeResultFailed:
            NSLog(@"Result: SMS sending failed");
            break;
        default:
            NSLog(@"Result: SMS not sent");
            break;
    }

    [self dismissViewControllerAnimated:YES completion:NULL];
}

编辑1:

我的.h看起来像这样:

#import <UIKit/UIKit.h>
#import <MessageUI/MFMessageComposeViewController.h>

@interface MIKETableViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate, MFMessageComposeViewControllerDelegate>

-(void)viewWillAppear:(BOOL)animated;
-(void) sendSMS;



@end

编辑2:

好的我尝试通过按下按钮启动sendSMS方法,一切正常,所以有一个问题,我如何调用该方法。事情是我从myCustomTableCell类调用方法...想法是,当我滑动屏幕的单元格它将调用sendSMS方法。请参阅自定义单元格中的.m文件

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

    MIKETableViewController *mainController = [[MIKETableViewController 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!");


                             self.timeLabel.text = timeLabelValue;
                             self.priceLabel.text = priceLabelValue;
                             self.infoLabel.text = infoLabelValue;

                             [mainController tableView:mainController.self.tableView didSelectRowAtIndexPath:mainController.self.tableView.indexPathForSelectedRow];

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

2 个答案:

答案 0 :(得分:0)

它应该可以工作,但是你的代码看起来很合适,所以很可能你没有符合委托<MFMessageComposeViewControllerDelegate>

@Janak Nirmal:对不起,我很快就看不到委托方法的实施。

将断点放在委托方法中,看看控件是否来自该方法。

在sendSMS方法中使用此代码显示:

[self presentViewController:controller animated:YES completion:nil];

现在检查一下。只有当按下发送或取消按钮时,才会调用该代表。

修改

问题在于这个方法:

presentViewController:animated:onComplete:

您在同一个控制器上呈现的控制器需要解除。

例如:

[self presentViewController:viewController animated:YES onComplete:nil];
[self dismissViewControllerAnimated:YES completion:nil];

   [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:controller animated:animated completion:callback];

    [[UIApplication sharedApplication].delegate.window.rootViewController
dismissViewControllerAnimated:YES completion:nil];

修改

将此内容导入.h文件

#import <MessageUI/MessageUI.h> 

添加此头文件并检查是否正在调用该委托。

修改

所以你这样说:

messageComposeViewController:didFinishWithResult:

这个委托方法即使现在也没有被点击发送或取消????

修改

而不是这行代码

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

把这段代码告诉我是否调用了委托方法

UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window]rootViewController];

        [rootViewController presentViewController:viewController animated:YES completion:nil];

修改

唯一的方法是,让我在VCS的某个地方访问您的代码,我将帮助您解决问题。

答案 1 :(得分:0)

您在邮件编辑器委托方法中解雇self,而不是邮件视图控制器,更改:

[self dismissViewControllerAnimated:YES completion:NULL];

[controller dismissViewControllerAnimated:YES completion:NULL];