【问题标题】:NSNotificationCenter object UILabelNSNotificationCenter 对象 UILabel
【发布时间】:2014-02-21 23:47:51
【问题描述】:

这段代码有什么问题...

[[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(MySubViewNotification:) name:@"MySubViewNotification" object:nil];

 -(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'

【问题讨论】:

    标签: objective-c ios7 xcode5 nsnotificationcenter


    【解决方案1】:

    通知调用的方法应该有签名:

    - (void)mySubViewNotification:(NSNotification *)note
    

    (注意小写第一个字母 - 标准 - 类名以大写开头,方法以小写开头,相应地更新您的代码,并且参数是通知)

    然后可以在方法中添加:

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

    因为通知是作为参数传递给您的,您需要从中获取包含的信息(本例中为标签)。

    【讨论】:

    • 谢谢,但添加通知错误 [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MySubViewNotification:) name:@"MySubViewNotification" object:nil];
    • 我不明白你的评论。
    • 当我创建通知时,[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MySubViewNotification:) name:@"MySubViewNotification" object:nil];我有 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[MasterViewController MySubViewNotification:]:无法识别的选择器发送到实例 0x15d4bcf0”
    • 因为你没有更正命名标准的方法名称(小写字母)developer.apple.com/library/mac/documentation/Cocoa/Conceptual/…
    猜你喜欢
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    相关资源
    最近更新 更多