我在View Controller的viewDidLoad方法实现中设置了文本消息窗口。如果特定的NSString对象包含“ YES”,那么我将调用我的文本消息窗口的方法并弹出。

所有这些工作都很好,唯一的问题是我的文本消息方法调用实际上是viewDidLoad中的第一件事,但是由于某种原因,在执行完viewDidLoad中的其他所有内容之后,文本消息窗口就会弹出。

在我的viewDidLoad中,在文本消息窗口代码的下面,我设置并启动一个AVCapture会话,还创建了一个“预览层”来显示摄像机看到的内容。

即使此代码位于文本消息窗口代码的下方,您也可以在弹出文本消息窗口之前看到摄像机的预览层几秒钟。

这是我的viewDidLoad方法实现。请注意,我的文本消息窗口方法[self showSMS]的调用是如何先于其他一切的:

- (void)viewDidLoad
{

    [super viewDidLoad];

    if([_justFinishedSigningUp isEqual:@"YES"]) {

        [self showSMS];
    }



    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
        // iOS 7
        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    } else {
        // iOS 6
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    }


    _session =[[AVCaptureSession alloc]init];


    [_session setSessionPreset:AVCaptureSessionPresetPhoto];


    _inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];


    NSError *error;


    _deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:_inputDevice error:&error];


    if([_session canAddInput:_deviceInput])
        [_session addInput:_deviceInput];


    _previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:_session];


    CALayer *rootLayer = [[self view]layer];

    [rootLayer setMasksToBounds:YES];


    [_previewLayer setFrame:CGRectMake(0, 0, rootLayer.bounds.size.width, rootLayer.bounds.size.height/2)];

    [_previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];


    [rootLayer insertSublayer:_previewLayer atIndex:0];


    _stillImageOutput = [[AVCaptureStillImageOutput alloc] init];


    [_session addOutput:_stillImageOutput];

    [_session startRunning];

    }


然后是有助于控制文本消息窗口的方法实现:

- (void)showSMS {

    if(![MFMessageComposeViewController canSendText]) {
        UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device doesn't support SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [warningAlert show];
        return;
    }

    NSArray *recipents = _usersToInviteToApp;
    NSString *message = _textMessageInviteText;

    MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
    messageController.messageComposeDelegate = self;
    [messageController setSubject:@"New Message"];
    [messageController setRecipients:recipents];
    [messageController setBody:message];

    // Present message view controller on screen
    [self presentViewController:messageController animated:YES completion:nil];
}


- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
    switch (result) {
        case MessageComposeResultCancelled:
            break;

        case MessageComposeResultFailed:
        {
            UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to send SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [warningAlert show];
            break;
        }

        case MessageComposeResultSent:
            break;

        default:
            break;
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}

最佳答案

您正在使用animated:YES中的viewDidLoad将某些内容加载到视图中,这是在屏幕上还没有任何内容之前发生的([self presentViewController:messageController animated:YES completion:nil];

您是否尝试过将所有内容移至viewDidAppear,或者,如果要在所有内容都未显示之前就将屏幕移至该位置,请尝试在animated:NO方法中设置-(void)showSMS

08-05 23:42