0

私はUIViewController別のものをもたらすボタンを持っていますUIViewController。my に示すように、ボタンをクリックしてNSLog、これが完了したら、通知を送信して別のviewcontroller. さて、すべてが正しく行われているように見えますが、どういうわけか機能せず、表示されませんUIViewController。コードは次のとおりです。

 [[NSNotificationCenter  defaultCenter] addObserver:self selector:@selector(infoPage:)
                                                  name:@"InfoPage" object:nil ];



-(void) infoPage:(NSNotification*)notification
{
    NSLog(@"Code executing in Thread %@",[NSThread currentThread] );

    InfoCtrol *i = [[InfoCtrol alloc] init];
     i.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:i animated:YES];
}

私のタブバアイテムボタン

-(void)info {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"InfoPage"
                                                        object:nil
                                                      userInfo:nil];
     NSLog(@"test not");
}

私の問題は次のとおりだと思います: mainThread にはありませんが、それをどのように解決すればよいかわかりません:

私もこれを使用しましたが、UIViewController をもたらしませんでした:

[self performSelectorOnMainThread:@selector(test) withObject:nil waitUntilDone:NO];

-(void)test{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"InfoPage"
                                                        object:nil
                                                      userInfo:nil];
}

このコードをボタンに入れるとUIViewController、が表示されますが、使用したいNSNotificationCenter

InfoCtrol *i = [[InfoCtrol alloc] init];
     i.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:i animated:YES];

私のログ:

Code executing in Thread <NSThread: 0x1fd7c7e0>{name = (null), num = 1}

アップデート:

 How should i remove last thread from mainThread
4

1 に答える 1

0

アクションを問題なく直接実行できるのに、なぜここで通知を使用したいのかわかりません。ただし、UI を更新する必要がある通知メソッドで実行できる簡単なことは、メイン スレッドでまだ実行されていない場合は、メイン スレッドで自分自身を呼び出すようにすることです。

-(void)myNotificationMethod:(NSNotification*)note {
    if (![NSThread isMainThread]) {
        [self performSelectorOnMainThread:@selector(myNotificationMethod:)
                               withObject:note
                            waitUntilDone:NO];
        return;
    }

    // ... do some UI stuff
    InfoCtrol *i = [[InfoCtrol alloc] init];
    [self.navigationController pushViewController:i animated:YES];
}
于 2013-03-24T04:52:32.957 に答える