AlertView委托与UIBackgroundFetchResult崩溃的应用程序

时间:2014-11-25 12:10:23

标签: ios objective-c iphone xcode5

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

以下是我的应用的故事板,红色箭头是执行fetchNewDataWithCompletionHandler代码的地方。

screen shot, note the arrow is displayed where the code fetchNewDataWithCompletionHandler is being executed

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按钮,应用程序就会崩溃。

正如您在我的应用程序中看到的,有四个选项卡:主页,产品,朋友和帐户。 即使我在模拟器中选择了“朋友”选项卡,然后进行后台获取过程,它仍然会崩溃。 我也尝试使用selectedIndex切换到标签栏,但这也无效。

我被困,失去了,请指教,谢谢。

1 个答案:

答案 0 :(得分:0)

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

@interface demo_Friends_ViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate, UIAlertViewDelegate>