UINavigationItem setLeftBarButtonItems Delay
UINavigationItem setLeftBarButtonItems Delay
我有一个相当混乱的问题:在我的应用程序中,我正在检查网络连接,如果用户没有连接到互联网,我会在导航栏中显示一个红色的小感叹号。
用于显示感叹号的代码如下所示:
- (void)showConnectivityMessage
{
UIBarButtonItem *exclamationMark = [[UIBarButtonItem alloc]initWithTitle:@"!" style:UIBarButtonItemStyleDone target:self action:@selector(showConnectivityInfo)];
[exclamationMark setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont boldSystemFontOfSize:22]} forState:UIControlStateNormal];
NSMutableArray *items = [NSMutableArray arrayWithArray:self.navigationItem.leftBarButtonItems];
[items addObject:exclamationMark];
[self.navigationItem setLeftBarButtonItems:items];
NSLog(@"Should show exclamation mark!");
}
方法调用正确,我可以看到输出"Should show exclamation mark!"。现在,奇怪的是,新的导航项实际出现还需要大约 20 秒...
有谁知道这种延迟可能来自哪里?任何解决错误的帮助将不胜感激!
有些代码阻塞了主线程。试试这个。可能对你有帮助
- (void)showConnectivityMessage
{
dispatch_async(dispatch_get_main_queue(), ^{
UIBarButtonItem *exclamationMark = [[UIBarButtonItem alloc]initWithTitle:@"!" style:UIBarButtonItemStyleDone target:self action:@selector(showConnectivityInfo)];
[exclamationMark setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont boldSystemFontOfSize:22]} forState:UIControlStateNormal];
NSMutableArray *items = [NSMutableArray arrayWithArray:self.navigationItem.leftBarButtonItems];
[items addObject:exclamationMark];
[self.navigationItem setLeftBarButtonItems:items];
NSLog(@"Should show exclamation mark!");
});
}
每次您在 UI 中看到类似的延迟,那是因为您还没有从主线程更新 UI。始终仅从主线程更新 UI。
dispatch_async(dispatch_get_main_queue(), ^{
UIBarButtonItem *exclamationMark = [[UIBarButtonItem alloc]initWithTitle:@"!" style:UIBarButtonItemStyleDone target:self action:@selector(showConnectivityInfo)];
[exclamationMark setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont boldSystemFontOfSize:22]} forState:UIControlStateNormal];
NSMutableArray *items = [NSMutableArray arrayWithArray:self.navigationItem.leftBarButtonItems];
[items addObject:exclamationMark];
[self.navigationItem setLeftBarButtonItems:items];
NSLog(@"Should show exclamation mark!");
}
我有一个相当混乱的问题:在我的应用程序中,我正在检查网络连接,如果用户没有连接到互联网,我会在导航栏中显示一个红色的小感叹号。
用于显示感叹号的代码如下所示:
- (void)showConnectivityMessage
{
UIBarButtonItem *exclamationMark = [[UIBarButtonItem alloc]initWithTitle:@"!" style:UIBarButtonItemStyleDone target:self action:@selector(showConnectivityInfo)];
[exclamationMark setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont boldSystemFontOfSize:22]} forState:UIControlStateNormal];
NSMutableArray *items = [NSMutableArray arrayWithArray:self.navigationItem.leftBarButtonItems];
[items addObject:exclamationMark];
[self.navigationItem setLeftBarButtonItems:items];
NSLog(@"Should show exclamation mark!");
}
方法调用正确,我可以看到输出"Should show exclamation mark!"。现在,奇怪的是,新的导航项实际出现还需要大约 20 秒...
有谁知道这种延迟可能来自哪里?任何解决错误的帮助将不胜感激!
有些代码阻塞了主线程。试试这个。可能对你有帮助
- (void)showConnectivityMessage
{
dispatch_async(dispatch_get_main_queue(), ^{
UIBarButtonItem *exclamationMark = [[UIBarButtonItem alloc]initWithTitle:@"!" style:UIBarButtonItemStyleDone target:self action:@selector(showConnectivityInfo)];
[exclamationMark setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont boldSystemFontOfSize:22]} forState:UIControlStateNormal];
NSMutableArray *items = [NSMutableArray arrayWithArray:self.navigationItem.leftBarButtonItems];
[items addObject:exclamationMark];
[self.navigationItem setLeftBarButtonItems:items];
NSLog(@"Should show exclamation mark!");
});
}
每次您在 UI 中看到类似的延迟,那是因为您还没有从主线程更新 UI。始终仅从主线程更新 UI。
dispatch_async(dispatch_get_main_queue(), ^{
UIBarButtonItem *exclamationMark = [[UIBarButtonItem alloc]initWithTitle:@"!" style:UIBarButtonItemStyleDone target:self action:@selector(showConnectivityInfo)];
[exclamationMark setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont boldSystemFontOfSize:22]} forState:UIControlStateNormal];
NSMutableArray *items = [NSMutableArray arrayWithArray:self.navigationItem.leftBarButtonItems];
[items addObject:exclamationMark];
[self.navigationItem setLeftBarButtonItems:items];
NSLog(@"Should show exclamation mark!");
}