1

我正在使用 Xcode 4.5 并针对 iOS 5 及更高版本。
我有一个弹出框,允许用户更改底层视图的背景。

点击“按钮”时,需要我点击两次才能看到更改。
我正在使用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];
}
4

1 回答 1

0

如果您使用的是故事板,并且您的弹出框是一个 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....

}
于 2012-11-06T20:01:48.140 回答