NSOperationQueue:无法在主线程的工具栏中添加 UIBarButtonItem
在我的 UIViewController 中,我想在工具栏中添加 UIBarButtonItem,但新按钮没有出现。我做错了什么?
- (void)doLogin:(NSString *)name password:(NSString *)password {
// 1.: start the Thread:
NSInvocationOperation *invOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(backgroundTaskLogin:) object:request];
[self.opQueue addOperation:invOperation];
}
- (void)backgroundTaskLogin:(NSString *)request2 {
// 2.: jump back in the Main Thread in show a cancel button in den toolbar:
[self performSelectorOnMainThread:@selector(showCancelButton) withObject:nil waitUntilDone:NO];
}
- (void)showCancelButton {
// 3.: add a new Cancel-Button in the Toolbar:
UIBarButtonItem *tempButtonCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelLogin)];
NSMutableArray *myButtons = (NSMutableArray *)self.toolbarItems;
NSLog(@"Count buttons: %d", [self.toolbarItems count]); // DEBUGGER: 2
[myButtons addObject:tempButtonCancel];
[tempButtonCancel release];
NSLog(@"Count buttons: %d", [self.toolbarItems count]); // DEBUGGER: 3
// PROBLEM: I don't see the new Toolbar-Button :-(
}
In my UIViewController I want to add a UIBarButtonItem in the Toolbar, but the new Button doesn't appear. What am I doing wrong?
- (void)doLogin:(NSString *)name password:(NSString *)password {
// 1.: start the Thread:
NSInvocationOperation *invOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(backgroundTaskLogin:) object:request];
[self.opQueue addOperation:invOperation];
}
- (void)backgroundTaskLogin:(NSString *)request2 {
// 2.: jump back in the Main Thread in show a cancel button in den toolbar:
[self performSelectorOnMainThread:@selector(showCancelButton) withObject:nil waitUntilDone:NO];
}
- (void)showCancelButton {
// 3.: add a new Cancel-Button in the Toolbar:
UIBarButtonItem *tempButtonCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelLogin)];
NSMutableArray *myButtons = (NSMutableArray *)self.toolbarItems;
NSLog(@"Count buttons: %d", [self.toolbarItems count]); // DEBUGGER: 2
[myButtons addObject:tempButtonCancel];
[tempButtonCancel release];
NSLog(@"Count buttons: %d", [self.toolbarItems count]); // DEBUGGER: 3
// PROBLEM: I don't see the new Toolbar-Button :-(
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能依赖
self.toolbarItems
作为可变数组。如果您之前为该属性分配了一个可变数组,则可能是您的情况,但如果您不使用记录的接口,则不能指望视图控制器注意到属性的更改。创建一个新数组并使用 setter 将其分配给
toolbarItems
:You can't rely on
self.toolbarItems
being a mutable array. It may be one in your case if you assigned a mutable array to that property before but you can't expect the view controller to notice a change in a property if you don't use the documented interface.Create a new array and use the setter to assign it to
toolbarItems
:Ole 帖子中的代码将修复您的错误,但不是出于他建议的原因(因为看起来您正在成功地将一个项目添加到数组中,即使大多数情况下它不应该是可变的时间)。在复制和修改项目数组后,您必须调用
setToolbarItems:
,因为 UIToolbar 不会检测到对其数组进行的更改。您还可以使用
setToolbarItems:animated:
来很好地淡入淡出;-)The code in Ole's post will fix your bug, but not for the reason he suggests (since it appears that you are successfully adding an item to the array, even if that shouldn't be mutable most of the time). You have to call
setToolbarItems:
after duplicating and modifying the items array because the UIToolbar does not detect that changes have been made to it's array otherwise.You can also use
setToolbarItems:animated:
to fade things in nicely ;-)