【问题标题】:ViewWillAppear Getting Called twiceViewWillAppear 被调用两次
【发布时间】:2013-11-14 00:40:49
【问题描述】:

我正在使用故事板在 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 方法被调用了两次。您知道为什么会发生这种情况吗?因为这是通过向我的视图添加两次而不是仅一次来检查数据两次并在那里添加内容?

谢谢, 瓦伦

第一个 - ViewDidAppear 的 Stacktrace 是

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

)

2nd - 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: 每次调用的调用堆栈是什么?
  • @JefferyThomas 我为两个 viewDidAppear 调用添加了堆栈跟踪。希望这会有所帮助。
  • 您是否在RESideMenu 中包含GoogleMapsViewController?不知何故,RESideMenu 正在将通知传递给GoogleMapsViewController
  • 是的RESideMenu 有一个内容视图控制器,它是我的GoogleMapsViewController。我需要改变什么?

标签: ios objective-c uiviewcontroller viewwillappear


【解决方案1】:

我使用的是旧版本的第三方库,导致 viewdidappear 被调用了两次。通过更新库可以解决此问题。

【讨论】:

    【解决方案2】:

    如果你想让你的代码执行一次,你可以给一个布尔值。例如

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

    【讨论】:

    • 你的意思是像使用全局变量还是存储全局变量的 NSUserDefault 变量?是的,这可以工作,但我宁愿弄清楚它发生的原因,也不愿放入这样的黑客攻击。
    猜你喜欢
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多