이것은 매우 이상합니다. 내 견해는 태그로 UIbutton을 찾은 다음 텍스트 레이블을 변경하는 데 사용하는 색인 번호가있는 알림을 받고 있습니다. 유일한 문제는 텍스트가 변경되지 않는다는 것입니다. 브레이크 포인트를 넣었고 코드가 실행 중입니다.기발한 uibuttons 텍스트 레이블이 변경되지 않습니다
알림을 사용할 때 변수 범위와 관련하여 특이한 점이 있습니까? 내 기본보기 컨트롤러 내 reciveevent 기능이 기능은 메인 스레드에서 실행되지 않는 경우에도 (정말 이해 해달라고) 것으로 확인되었습니다 덧글 그래서 그게 전부에
- (void)receiveEvent:(NSNotification *)notification {
int pass = [[[notification userInfo] valueForKey:@"index"] intValue];
NSLog(@"button index is %i", pass);
UIButton *changebut = (UIButton *) [self.view viewWithTag:pass];
ButtonStatusModel *m= [self.buttoneventpendingarray objectAtIndex:pass];
NSLog(@"current but status = %i",m.btnstatus);
if (m.btnstatus==3) {
//this should change the label but it does not;
[changebut setTitle:@"playing" forState:UIControlStateNormal];
m.btnstatus=2;
NSLog(@"should set text to 'playing");
//[engine addActionToQue:buttonid actionSample:actionsample action:1];
}else if (m.btnstatus==1) {
//this should change the label but it does not;
[changebut setTitle:@"idle" forState:UIControlStateNormal];
NSLog(@"should set text to 'idle");
m.btnstatus=0;
//[engine addActionToQue:buttonid actionSample:actionsample action:0];
}
}
EDIT 1 감사합니다 버튼 레이블이 변경되지 않는 이유.
다음 코드를 사용하여 주 스레드인지 여부를 확인했습니다. 주 스레드에서 perform selector를 호출해야하는 버튼 레이블을 변경한다고 생각합니까? 이것이 올바른 일일까요? 그렇다면 함수 2 변수를 전달해야 할 필요가 있는데, 주 스레드에서 선택기를 수행하면이를 수용하는 방법을 알지 못합니다. 대신 주요 선택의
-(void) updatebutton:(int)tag changeto:(int) val
{
if ([NSThread isMainThread]) {
{
NSLog(@"is main thread");
}
} else {
NSLog(@"is not thread");
[self performSelectorOnMainThread:@selector(/*function with 2 parameters here*/) withObject:nil waitUntilDone:YES];
}
}
EDIT 3 *
사용하여 블록 내 문제에서 저를 도왔다.
나는 전체 주 스레드를 정말로 이해하지 못합니다. 코드가 내보기 컨트롤러에 있으면 실행 된 코드가 주 스레드에서 수행된다고 가정했습니다. 그것은 큰 뚱뚱한 것 같다. 나는이 문제에 대해 약간의 깨달음을 느낄 것이다.
Heres는 어쨌든 무엇을 사용
-(void) updatebutton:(int)tag changeto:(int) val
{
UIButton *changebut = (UIButton *) [self.view viewWithTag:tag];
if ([NSThread isMainThread]) {
{
NSLog(@"is main thread");
if (val==2) {
[changebut setTitle:@"playing" forState:UIControlStateNormal];
}
if (val==0) {
[changebut setTitle:@"idle" forState:UIControlStateNormal];
}
}
} else {
NSLog(@"is not thread");
//[self performSelectorOnMainThread:@selector(updatebutton::) withObject:nil waitUntilDone:YES];
if (val==2) {
dispatch_async(dispatch_get_main_queue(), ^{
[changebut setTitle:@"playing" forState:UIControlStateNormal];
});
}
if (val==0) {
dispatch_async(dispatch_get_main_queue(), ^{
[changebut setTitle:@"idle" forState:UIControlStateNormal];
});
}
}
}
'changebut'이'nil'인지 (캐스팅 한 후) 확인 했습니까? '-receiveEvent'가 주 스레드가 아닌 다른 스레드에서 호출 되었습니까? UI 변경은 주 스레드에서 수행해야합니다. –
changebut은 전송 한 후 '아님'이 아닙니다. recieveevent 기능은 내 메인 uiview에 살고있다. 알림은보기에서 생성되지 않습니다 – dubbeat
컨텍스트를 두 번 확인하는 방법은 기본 스레드입니까? – dubbeat