2012-04-27 73 views
0

我在我的设置页面中有一个UISwitchcontroller来改变主页面中的图像,所以我把从吃网页到主页面的通知..但只有一个通知我每次活跃,,我的设置页面切换代码看这样NSnotifiaction疑惑

-(IBAction)_clickswitchlowlight:(id)sender 
{ 

if(switchControll.on){ 

    [switchControll setOn:YES animated:YES]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_lowlighton object:nil]; 



} 
else{ 
    [switchControll setOn:NO animated:YES]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_lowlightoff object:nil]; 

} 
} 
在我的主要page.h..i

编写代码

extern NSString * const NOTIF_lowlighton; 
extern NSString * const NOTIF_lowlightoff; 

中的.m主要页面的执行上面我写这篇文章

NSString * const NOTIF_lowlighton = @"lowlighton"; 
NSString * const NOTIF_lowlightoff = @"lowlightoff"; 

在viewWillAppear中我写这篇文章的代码

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clicklowlighton:) name:@"lowlighton" object:nil]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clicklowlightoff:) name:@"lowlightoff" object:nil]; 
} 

那么这段代码改变图像

- (void)clicklowlighton:(NSNotification *)notif 
{ 
    [[self multiPageView] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bgipad"]]]; 

} 
- (void)clicklowlightoff:(NSNotification *)notif 
{ 
    [[self multiPageView] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"[email protected]"]]]; 

} 

我只得到clicklowlightoff通知,我没有拿到第一通知......在任何缺失我的代码? 预先感谢。

+0

你为什么改变开关的状态呢? [switchControll setOn:YES animated:YES]; ?? – 2012-04-27 10:53:29

+0

@Ubersoldat检查它是否打开..是一个错误? – stackiphone 2012-04-27 10:58:15

+0

不,我的意思是,当它已经处于“开”状态时,试图将其设置为“开”。为什么? – 2012-04-27 11:05:27

回答

0

你的函数事件绑定:UIControlEventValueChanged这个功能

-(IBAction)_clickswitchlowlight:(id)sender 
    { 

    if(switchControll.on){ 

     [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_lowlighton object:nil]; 
    } 
    else{ 

     [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_lowlightoff object:nil]; 

    } 
} 
+0

它无法正常工作,io在主页面的低光照中放置了一个警报视图..当开启或关闭它时显示警报.-(void)clicklowlightoff :(NSNotification *)notif { [[self multiPageView] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@“bglowlight @ 2x”]]]; //这里我把警报视图 } – stackiphone 2012-04-27 11:16:36

+0

你的函数绑定到事件:UIControlEventValueChanged这个函数是什么意思感谢 – stackiphone 2012-04-27 11:17:34

+0

拖放uicontroleventvaluechanged连接到这个功能,断开链接等 – 2012-04-27 12:30:23