簡體   English   中英

ViewWillAppear被調用兩次

[英]ViewWillAppear Getting Called twice

我正在使用故事板在iOS 7上編寫應用程序。

我有一個導航控制器作為我的根視圖控制器,並在其中嵌入了一個視圖控制器。

當應用程序加載時,viewDidLoad被調用一次,viewWillAppear被調用一次。 這是正確的(我也檢查數據並更新視圖中的視圖將出現)。

但是,在我的視圖控制器中,我有一個按鈕,允許用戶使用'MFMessageComposeViewController'控制器通過SMS /文本共享信息。

所以這里的代碼是

MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];

if([MFMessageComposeViewController canSendText])

{ 
    controller.body = [NSString stringWithFormat:@"Some Text Here"];

    controller.messageComposeDelegate = self;

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

和委托函數是

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
 switch (result) {
      case MessageComposeResultCancelled:
           NSLog(@"Cancelled");
           break;
      case MessageComposeResultSent:
           NSLog(@"Message Sent");
           break;
      default:
           break;
 }

[self dismissViewControllerAnimated:YES completion:nil];
//[controller dismissViewControllerAnimated:YES completion:nil]; //tried this as well but same result

}

但是,當我調用dismissViewControllerAnimated ..時,viewDidAppear方法被調用了兩次。 您是否知道為什么會發生這種情況,因為它要檢查數據兩次,然后在視圖中兩次添加而不是一次添加?

謝謝,瓦倫

1-ViewDidAppear的堆棧跟蹤為

0   Sweep                               0x0010a8f1 -[GoogleMapsViewController viewDidAppear:] + 56
1   UIKit                               0x2fd3673f <redacted> + 410
2   UIKit                               0x2fde6657 <redacted> + 182
3   UIKit                               0x2fd3673f <redacted> + 410
4   UIKit                               0x2fd36bcd <redacted> + 264
5   Sweep                               0x000e9909 -[RESideMenu viewDidAppear:] + 88
6   UIKit                               0x2fd3673f <redacted> + 410
7   UIKit                               0x2fd36bcd <redacted> + 264
8   UIKit                               0x2fe3321b <redacted> + 1682
9   UIKit                               0x2fe32ab7 <redacted> + 170
10  UIKit                               0x2fe329e3 <redacted> + 74
11  UIKit                               0x2fe328c9 <redacted> + 288
12  UIKit                               0x2fe323d9 <redacted> + 944
13  UIKit                               0x2fd53ab7 <redacted> + 178
14  UIKit                               0x2fd539cf <redacted> + 66
15  QuartzCore                          0x2f9a9413 <redacted> + 234
16  libdispatch.dylib                   0x37dd90af <redacted> + 22
17  libdispatch.dylib                   0x37ddb9a9 _dispatch_main_queue_callback_4CF + 268
18  CoreFoundation                      0x2d5625b1 <redacted> + 8
19  CoreFoundation                      0x2d560e7d <redacted> + 1308
20  CoreFoundation                      0x2d4cb471 CFRunLoopRunSpecific + 524
21  CoreFoundation                      0x2d4cb253 CFRunLoopRunInMode + 106
22  GraphicsServices                    0x322052eb GSEventRunModal + 138
23  UIKit                               0x2fd80845 UIApplicationMain + 1136
24  Sweep                               0x00098299 main + 116
25  libdyld.dylib                       0x37dedab7 <redacted> + 2

2-ViewDidAppear的Stacktrace是

0   Sweep                               0x0010a8f1 -[GoogleMapsViewController viewDidAppear:] + 56
1   UIKit                               0x2fd3673f <redacted> + 410
2   UIKit                               0x2fde6657 <redacted> + 182
3   UIKit                               0x2fd3673f <redacted> + 410
4   CoreFoundation                      0x2d4e0803 <redacted> + 50
5   CoreFoundation                      0x2d4da21d <redacted> + 220
6   UIKit                               0x2fd3687b <redacted> + 726
7   UIKit                               0x2fd36bcd <redacted> + 264
8   UIKit                               0x2fe3321b <redacted> + 1682
9   UIKit                               0x2fe32ab7 <redacted> + 170
10  UIKit                               0x2fe329e3 <redacted> + 74
11  UIKit                               0x2fe328c9 <redacted> + 288
12  UIKit                               0x2fe323d9 <redacted> + 944
13  UIKit                               0x2fd53ab7 <redacted> + 178
14  UIKit                               0x2fd539cf <redacted> + 66
15  QuartzCore                          0x2f9a9413 <redacted> + 234
16  libdispatch.dylib                   0x37dd90af <redacted> + 22
17  libdispatch.dylib                   0x37ddb9a9 _dispatch_main_queue_callback_4CF + 268
18  CoreFoundation                      0x2d5625b1 <redacted> + 8
19  CoreFoundation                      0x2d560e7d <redacted> + 1308
20  CoreFoundation                      0x2d4cb471 CFRunLoopRunSpecific + 524
21  CoreFoundation                      0x2d4cb253 CFRunLoopRunInMode + 106
22  GraphicsServices                    0x322052eb GSEventRunModal + 138
23  UIKit                               0x2fd80845 UIApplicationMain + 1136
24  Sweep                               0x00098299 main + 116
25  libdyld.dylib                       0x37dedab7 <redacted> + 2

我使用的是第三方庫的舊版本,這導致viewdidappear被調用了兩次。 通過更新庫可以解決此問題。

如果您希望代碼執行一次,則可以指定一個Bool。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!_loaded)
    {
       _loaded = YES;
       ///do want you wanna to do.
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM