2013-04-17 17 views
0

我使用的線程調用我的函數「initialGetMethod」調用通過線程即給出一個例外

[NSThread detachNewThreadSelector:@selector(initialGetMethod) toTarget:self withObject:nil]; 

和我的get方法是

-(void) initialGetMethod 
{ 
    self.loginPassword = [[ UIAlertView alloc] initWithTitle:@"Please Login to MFP" message:@"Enter Valid UserID and Password" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil]; 
    [self.loginPassword setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; 
    [self.loginPassword setTag:2]; 
    [self.loginPassword show]; 
} 

但它給出了這個異常「試過從主線程或Web線程以外的線程獲取Web鎖定,這可能是從輔助線程調用UIKit的結果「

它在」[self.loginPassword se tAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];」

,如果我調用該函數爲:「[自initialGetMethod];」它不給例外,但它會需要一些時間..

我在後臺加載嘗試,但它不工作。(意思是我不希望它是在後臺)..

請提出了一些解決方案。 。

+3

你不能在後臺線程執行的UI操作,你爲什麼想? – Gary

+0

這爲什麼用iPhone-sdk-4.0標記?我希望你沒有試圖支持iOS 4.0。 – rmaddy

+0

我試圖用 「[NSThread detachNewThreadSelector:@selector(initialGetMethod)toTarget:自withObject:無];」但仍然得到相同的例外.. – Raju

回答

2

您正在運行的應用程序時得到的錯誤是

「試圖獲得來自除主線程或web線程以外的線程網絡鎖定,這可能是從打電話來的UIKit的結果副螺紋。「

這是發生一次要更新或訪問任何其他線程的UI元素,除了MainThread(使用主線程只能訪問或更新UI它只會幫你)

在這裏,您都出現在背景警報線程,這就是爲什麼它發生

請使用下列之一彈出警報

[self performSelector:@selector(initialGetMethod) withObject:nil]; 

    [self performSelector:@selector(initialGetMethod) withObject:nil afterDelay:0.1]; 
+0

感謝您的答案,但第二個解決方案部分工作.. – Raju

-1
//call createthread: 

    [self setupTimerThread:]; 



     //write following code in setupTimerThread 
     timer = [NSTimer timerWithTimeInterval:02 
             target:self 
             selector:@selector(initialGetMethod:) 
             userInfo:nil 
             repeats:NO]; 
     NSRunLoop* runLoop = [NSRunLoop currentRunLoop]; 
     [runLoop addTimer:timer forMode:NSRunLoopCommonModes]; 
     [runLoop run]; 
+0

謝謝你的答案...但我沒有得到解決這個問題代碼.. – Raju

+0

試試我編輯的答案。它應該工作,如果問題是由於後臺UI更新。 – Anil

+0

感謝..但還是它不工作.. – Raju

相關問題