【问题标题】:Method not running - iOS方法未运行 - iOS
【发布时间】:2014-06-14 05:10:28
【问题描述】:

我有一个非常简单的方法。但是在方法中 UIAlertView 不会运行....这是我的方法:

-(void)post_result {
    NSLog(@"Post Result");

    post_now.enabled = YES;
    [active stopAnimating];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Success" message:@"You're post has been successfully uploaded." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [alertView show];

    success_post_facebook = 0;
    success_post_youtube = 0;
    success_post_googleplus = 0;
    success_post_tumblr = 0;
    success_post_twitter = 0;

    NSLog(@"Post Result END");
}

奇怪的是,UIAlertView 前后的代码都会在这个方法中运行....那么到底是怎么回事??

感谢您的宝贵时间。

【问题讨论】:

  • 调试器中有什么东西吗?您是否在单独的线程中调用此“post_result”?如果您希望得到一个好的答案,我会发布更多代码。
  • 它不会运行是什么意思 - 它不显示吗?
  • 确保在主线程上显示警报。
  • 你怎么打电话给post_result
  • 是的@rmaddy 你是对的。我通过内置的 Twitter API 从 Twitter API 请求中调用该程序。这必须使该方法在单独的线程中运行。谢谢。

标签: ios objective-c cocoa-touch methods uialertview


【解决方案1】:

您很可能不在主线程上调用 UIAlertView。要让它在主线程上运行,只需像这样使用主调度队列。

dispatch_async(dispatch_get_main_queue(), ^{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Success" message:@"You're post has been successfully uploaded." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [alertView show];
});

【讨论】:

  • 不应该是dispatch_sync()吗?
  • 谢谢,你也可以做 [[NSOperationQueue mainQueue] addOperationWithBlock: 我想。问题是我通过内置的 Twitter 框架从 Twitter API 调用中调用该方法。我认为这是让它在单独的线程上运行。
  • @trojanfoe 它只需要dispatch_sync 是您希望后台线程阻塞而调度的代码在主队列上运行。在这种情况下没有意义。
  • @Supertecnoboff 您可以使用 NSOperationQueue 但在这种情况下确实不需要。 Apple 建议您将 GCD 用于此类事情。 NSOperations 应该主要用于如果你想在你的情况下不需要的单独线程上启动/停止/恢复事情。
  • @Pete42 嗯,好的。非常感谢您提供详细信息。确实很有帮助。
猜你喜欢
  • 2018-07-28
  • 1970-01-01
  • 2021-08-13
  • 1970-01-01
  • 1970-01-01
  • 2012-10-02
  • 1970-01-01
  • 2020-08-02
相关资源
最近更新 更多