2012-11-05 47 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

虽然这种方法不适用于这个特定的问题,但我发现它适用于类似的问题。谢谢。 –