2014-02-21 27 views
0

什麼錯這個代碼...NSNotificationCenter對象的UILabel

[NSNotificationCenter defaultCenter]的addObserver:自我選擇:@選擇(MySubViewNotification :)名:@ 「MySubViewNotification」 對象:無]。

-(void)MySubViewNotification:(UILabel*)GenericLabel 
{ 

    GenericLabel.textColor = [UIColor whiteColor]; ---- Error ---- 
    GenericLabel.text = @"cwxcwxc"; ---- Error ---- 

    NSLog(@"%@", GenericLabel); 
} 

NSLog. 

NSConcreteNotification 0x1759c6c0 {name = MySubViewNotification; object = <UILabel: 0x175aace0; frame = (175 5; 62 15); text = '1.35'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x175aad80>>} 

Errror 

-[NSConcreteNotification setText:]: unrecognized selector sent to instance 0x165a4270 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteNotification setText:]: unrecognized selector sent to instance 0x165a4270' 

回答

0

你的方法,通過該通知被稱爲應有的簽名:

- (void)mySubViewNotification:(NSNotification *)note 

(注意小寫首字母 - 標準 - 類名稱開頭大寫,用小寫的方法,相應地更新你的代碼和參數是通知)

那麼你可以添加,在方法:

UILabel *label = note.object; 
label.textColor = [UIColor whiteColor]; 

,因爲通知是作爲參數傳遞給您的,您需要從中獲取附帶的信息(本例中爲標籤)。

+0

感謝但添加通知錯誤 [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MySubViewNotification :) name:@「MySubViewNotification」object:nil]; – Alan10977

+0

我不明白你的意見。 – Wain

+0

當我創建我的通知時,[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MySubViewNotification :) name:@「MySubViewNotification」object:nil];我有***終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:' - [MasterViewController MySubViewNotification:]:無法識別的選擇器發送到實例0x15d4bcf0' – Alan10977