2014-11-25 22 views
1

我試圖在應用程序處於後臺時獲取新的朋友請求。 抓取工作正常,它的alertView導致應用程序崩潰。我相信它是因爲我使用的代理值不正確,當我使用delegate:nil時,應用程序不會崩潰。AlertView Delegate with UIBackgroundFetchResult崩潰應用程序

這裏是我的應用程序的故事板,紅色箭頭是執行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切換到標籤欄,但那也不起作用。

我被卡住了,請指教,謝謝。

回答

0

在demo_Friends_ViewController.h,你忘了UIAlertViewDelegate;)

@interface demo_Friends_ViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate, UIAlertViewDelegate> 
+0

謝謝,剛纔添加,但仍死機一樣。 – user3550458 2014-11-25 14:14:05

+0

您是否找到解決方案或者仍然沒有? – Atomnium 2014-11-25 22:53:34

+0

好吧,我切換到正在工作的NSNotificationCenter,就像我想要的一樣。 – user3550458 2014-11-26 13:48:48