NSOperationQueue:无法在主线程的工具栏中添加 UIBarButtonItem

发布于 2024-10-02 17:16:06 字数 1185 浏览 0 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不奢求什么 2024-10-09 17:16:06

您不能依赖 self.toolbarItems 作为可变数组。如果您之前为该属性分配了一个可变数组,则可能是您的情况,但如果您不使用记录的接口,则不能指望视图控制器注意到属性的更改。

创建一个新数组并使用 setter 将其分配给 toolbarItems

NSMutableArray *newToolbarItems = [NSMutableArray arrayWithArray:self.toolbarItems];
[newToolbarItems addObject:tempButtonCancel];
self.toolbarItems = newToolbarItems;

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:

NSMutableArray *newToolbarItems = [NSMutableArray arrayWithArray:self.toolbarItems];
[newToolbarItems addObject:tempButtonCancel];
self.toolbarItems = newToolbarItems;
简单 2024-10-09 17:16:06

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 ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文