2012-11-05 45 views
1

我正在使用Xcode 4.5並且定位iOS 5及更高版本。
我有一個彈出窗口,允許用戶更改底層視圖的背景。從popover刷新視圖

當點擊「按鈕」時,它需要點擊兩次以查看更改。
我正在使用NSNotification。我曾試過授權,但它什麼也沒做。
任何幫助或提示將不勝感激。

從viewWillAppear中:

// nav button is for background image selection 
    navigationBtn.tag = buttonTag; 
    [navigationBtn setBackgroundImage:[UIImage imageNamed:tempImage] forState:UIControlStateNormal]; 

    if (selectFlag) { 
     [navigationBtn addTarget:self action:@selector(BGSelected:) forControlEvents:UIControlEventTouchUpInside]; 
    } else { 
     navigationBtn.adjustsImageWhenHighlighted = NO; 
    } 

而對於選擇的方法:

- (void)BGSelected:(id)sender { 
    self.bgtag = [sender tag]; 
    SettingsDAO *settingsDao = [[SettingsDAO alloc] init]; 

    if ([self.currentView isEqualToString:@"New_Journal"]) 
    { 
     NSLog(@"Update Journals Background"); 
     [settingsDao updateJournalBackgroundId:self.journalId withBackgroundId:self.bgtag]; 
    } 
    // notification to let EntryView know the background has changed 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"aBackgroundChanged" 
                object:self]; 

    [settingsDao release]; 
} 

的NSNotification方法:

- (void)aBackgroundChanged:(NSNotification *)notification { 
    [self invalidate]; 
    [self reloadSettings]; 
} 
+0

通知和委託是處理這個問題的正確方法。您必須確保popover調用者正在監聽該通知。我建議做一個BackgroundChange一個NSString常量。或者,確保popover控制器中的委託不爲零。 – Leonardo

+0

我怎樣才能使通知一個NSString常量,爲什麼這將是必要的?調用者正在監聽...我在ViewDidLoad中有一個觀察者。它應該在別的地方,因爲視圖是在popover被調用之前加載的? –

+0

像這樣做一個常量:NSString * const BackgroundChange = @「aBackgroundChange」;它會避免誤拼,最重要的是Xcode可以爲你自動完成。嘗試在創建彈出窗口後立即設置監聽器或將調用方分配爲委託。 – Leonardo

回答

0

如果使用的是故事板,和你popover是一個segue,你可以這樣做:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    // perform all the operations...ecc 
    [[(UIStoryboardPopoverSegue*)segue popoverController] setDelegate:self]; 
} 

然後,你必須確保你的調用者控制器具有所有的委託方法。

或者與NSNotification:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    // perform all the operations...ecc 
    [[NSNotificationCenter defaultCenter] 
     addObserver:self 
     selector:@selector(changeBackground:) 
     name:BackgroundHasChangedNotification 
     object:[segue destinationViewController]]; 
} 

然後只記得在dealloc中刪除觀察員,並在換背景方法:

-(void)changeBackground:(NSNotification*)notification { 

    NSDictionary *userInfo = notification.userInfo; 
    // I assume you are using background name 
    NSString *backgroundName = [userInfo objectForKey:BackgroundOneConstant]; 
    // do the rest.... 

} 
+0

我找到了解決這個問題的方法...因爲我使用的是NSNotification,我嘗試添加[self viewDidAppear:YES];到通知方法。現在,它可以在第一次點擊時運作。感謝您的幫助,無論。 –

+0

雖然這種方法不適用於這個特定的問題,但我發現它適用於類似的問題。謝謝。 –