我在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];
}
答案 0 :(得分:1)
您正在使用animated:YES
中的viewDidLoad
向视图中加载某些内容,该内容会在屏幕上显示任何内容之前发生([self presentViewController:messageController animated:YES completion:nil];
)
您是否尝试将所有内容移至viewDidAppear
,或者如果您希望屏幕显示在所有内容之前,请尝试在animated:NO
方法中设置-(void)showSMS
。