【问题标题】:AlertView Delegate with UIBackgroundFetchResult Crashing app带有 UIBackgroundFetchResult 崩溃应用程序的 AlertView 委托
【发布时间】:2014-11-25 12:10:22
【问题描述】:

我正在尝试在应用处于后台时获取新的好友请求。 获取工作正常,它的 alertView 导致应用程序崩溃。我相信这是因为我使用的委托值不正确,当我使用 delegate:nil 时,应用程序不会崩溃。

这是我的应用程序的情节提要,红色箭头是执行 fetchNewDataWithCompletionHandler 代码的位置。

AppDelegate.m

#import "demo_AppDelegate.h"
#import "demo_Friends_ViewController.h"


@implementation demo_AppDelegate

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    NSDate *fetchStart = [NSDate date];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    demo_Friends_ViewController *friends_vc = (demo_Friends_ViewController *) [storyboard instantiateViewControllerWithIdentifier:@"friendsTab"];
    [friends_vc fetchNewDataWithCompletionHandler:^(UIBackgroundFetchResult result) {
        completionHandler(result);
        NSDate *fetchEnd = [NSDate date];
        NSTimeInterval timeElapsed = [fetchEnd timeIntervalSinceDate:fetchStart];
        NSLog(@"Background Fetch Duration: %f seconds", timeElapsed);
    }];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:2 * 1024 * 1024
                                                            diskCapacity:25 * 1024 * 1024
                                                                diskPath:nil];
    [NSURLCache setSharedURLCache:sharedCache];
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    return YES;
}

demo_Friends_ViewController.h

#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

@interface demo_Friends_ViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate>

-(void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;

@end

demo_Friends_ViewController.m

-(void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

    /* some code here for fetching */

    completionHandler(UIBackgroundFetchResultNewData);
    [UIApplication sharedApplication].applicationIconBadgeNumber = 1;

UIAlertView *alertView = [[UIAlertView alloc]
                                       initWithTitle:@"New Friend Request!"
                                       message:responseObject[@"message"]
                                       delegate:self
                                       cancelButtonTitle:@"Ignore"
                                       otherButtonTitles:@"Accept", nil] ;
             alertView.tag = 2;
             [alertView show];

那么我有这个方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {................}

但编译器似乎找不到didDismissWithButtonIndex 代码方法。因为只要我按下任何Accept or Ignore 按钮,应用程序就会崩溃。

正如您在我的应用中看到的那样,有四个选项卡:主页、产品、朋友和帐户。 即使我在模拟器中选择 Friends 选项卡,然后选择 Background Fetch Process,它仍然会崩溃。 我也尝试使用 selectedIndex 切换到标签栏,但这也不起作用。

我迷路了,请指教,谢谢。

【问题讨论】:

    标签: ios objective-c iphone xcode5


    【解决方案1】:

    在 demo_Friends_ViewController.h 中,您忘记了 UIAlertViewDelegate ;)

    @interface demo_Friends_ViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate, UIAlertViewDelegate>
    

    【讨论】:

    • 谢谢,刚刚加了,还是一样的crash。
    • 您找到解决方案了吗?
    • 好吧,我切换到了 NSNotificationCenter,它的工作方式和我想要的一样。
    猜你喜欢
    • 1970-01-01
    • 2011-02-13
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多