objective c - NSOperationQueue: Can't add UIBarButtonItem in the Toolbar on Main Thread -
in uiviewcontroller want add uibarbuttonitem in toolbar, new button doesn't appear. doing wrong?
- (void)dologin:(nsstring *)name password:(nsstring *)password { // 1.: start thread: nsinvocationoperation *invoperation = [[nsinvocationoperation alloc] initwithtarget:self selector:@selector(backgroundtasklogin:) object:request]; [self.opqueue addoperation:invoperation]; } - (void)backgroundtasklogin:(nsstring *)request2 { // 2.: jump in main thread in show cancel button in den toolbar: [self performselectoronmainthread:@selector(showcancelbutton) withobject:nil waituntildone:no]; } - (void)showcancelbutton { // 3.: add new cancel-button in 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: don't see new toolbar-button :-( }
you can't rely on self.toolbaritems
being mutable array. may 1 in case if assigned mutable array property before can't expect view controller notice change in property if don't use documented interface.
create new array , use setter assign toolbaritems
:
nsmutablearray *newtoolbaritems = [nsmutablearray arraywitharray:self.toolbaritems]; [newtoolbaritems addobject:tempbuttoncancel]; self.toolbaritems = newtoolbaritems;
Comments
Post a Comment