NSNotificationCenter (NSUserDefaultsDidChangeNotification)问题?

我有一个退出登录按钮,然后用NSNotificationCenter来监听,代码如下

#import "Tab4ViewController.h"

@interface Tab4ViewController ()
{
    UILabel *_phoneLabel;
}
@property (nonatomic, strong) NSString *loginString;

@end

@implementation Tab4ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(infoAcion) name:NSUserDefaultsDidChangeNotification object:nil];

    [self addLogOutButton];
    [self addPhoneLabel];
}

- (void)addLogOutButton
{
    UIButton *logOutBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    logOutBtn.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2., 200);
    logOutBtn.backgroundColor = [UIColor redColor];
    [logOutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [logOutBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [logOutBtn.layer setCornerRadius:8.0];

    [self.view addSubview:logOutBtn];
    [logOutBtn addTarget:self action:@selector(logOutBtnTapped) forControlEvents:UIControlEventTouchUpInside];
}

- (void)infoAcion
{
    if ([[[User sharedInstance].userinfoDict objectForKey:@"hadLogin"] isEqualToString:@"NO"]) {
        _phoneLabel.text = @"请登录";

    }
    else
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        _phoneLabel.text = [defaults stringForKey:@"telephone"];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSUserDefaultsDidChangeNotification object:nil];
}

- (void)logOutBtnTapped
{
    [[User  sharedInstance]  reloadUserInfo:[User sharedInstance].userinfoDict hadLogin:@"NO" isGuest:@"YES"];
    [ProgressHUD showSuccess:@"退出登录成功"];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"infoAcion" object:nil];
    NSLog(@"退出登录_[User sharedInstance] = %@", [User sharedInstance].userinfoDict);
}

- (void)addPhoneLabel
{
    _phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    _phoneLabel.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2., 90);
    _phoneLabel.backgroundColor = [UIColor lightGrayColor];
    _phoneLabel.textColor = [UIColor redColor];
    _phoneLabel.textAlignment = NSTextAlignmentCenter;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"telephone"]) {
        _phoneLabel.text = [defaults stringForKey:@"telephone"];
    }

    [self.view addSubview:_phoneLabel];
}

只有我第一次点击退出登录的时候 _phoneLabel.text才变化,之后必须要点击两次退出登录才有变化,为什么要点击第二次才有变化呢?

阅读 3.5k
1 个回答

你的用法错了
NSNotificationCenter 的使用
1,定义一个方法
-(IBACtion)shuchu{ }
2,对象注册,并附带信息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shuchu) name:@"NotificationName" object:nil];
3,发送通知信息
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationName" object:nil];
4,dealloc中删除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题