2013-02-07 88 views
2

這很刺激...... !!!uialertview不止一次致電

我GOOGLE了這個問題,發現了一些Relative Questions但不Satisfactory Answers

所以我有一個- (IBAction)方法,增加了一些UITextField's值來NSMutableArray"Add"按鈕被點擊。我只是試圖顯示UIAlertView,如果UITextField是

我的代碼:

- (IBAction)addButtonPressed:(id)sender 
{ 
     if ([textField1.text length]==0 || [textField2.text length]==0 || !someFlag) 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Please Enter Valid Data..." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
      [alert show]; 
      [alert release]; 
     } 
     else 
     { 
      // Code to add textField's value to Array. 
     } 
} 

我的問題:

每當我按「添加」 按鈕空文本框UIAlertView出現三次

1)首次出現與"Close"按鈕。 (我從來沒有添加...)它在一段時間內消失。

2)第二次這似乎與「OK」 按鈕。 (這就是我添加的...)當我按下「OK」按鈕時,它消失。

3)第三次它與一起出現「關閉」再次按鈕。當我按「關閉」按鈕時它消失。

編輯:

類似的問題:UIAlertView Pops Up Three Times per Call Instead of Just Once。 有人可以幫我找到解決辦法嗎?

+4

問題是與其他代碼不是這個代碼。檢查其他代碼。 –

+0

你可以顯示與「關閉」按鈕 –

+0

出現的alertView你有條件[textField.text isEqualToString:@「」] – KDeogharkar

回答

1

奇怪.... !!!

有時候碰巧你完全忽視你的代碼的某些行當你過惱火。它也發生在我身上。我忽略了從-addButtonPressed方法中調用的一種方法,其中有一個AlertView(當然是"Close"按鈕)。

這就是解決方案本身!

0

請嘗試下面的代碼......讓我知道它工作與否!

你做了什麼你給其他按鈕零2 times..so可以是問題...

編碼愉快!!!!

if ([textField.text length]==0) 
    { 

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Please Enter Valid Data..." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    } 
+0

是的,我試過,但不工作... – Bhavin

-1

請與下面的代碼:

if ([textField.text length]==0) 
{ 
UIAlertView *objAlertMsg = [[UIAlertView alloc] initWithTitle:@"MyApp" 
                 message:@"Please Enter Valid Data..." 
                 delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
[objAlertMsg show]; 
[objAlertMsg release]; 
} 

檢查,我已委託設爲 「無」,而不是 「自我」。如果不需要,請確保你沒有在視圖控制器中實現委託。

希望它對你有幫助。

乾杯。

+0

我試過這個,但沒有運氣......! – Bhavin

+0

PLZ檢查更新的代碼,並讓我知道它是否成功。 –

+0

是的,我試過,但沒有工作... – Bhavin

1

您的代碼不包含任何問題。沒有3它只有2個警報。這裏的警報視圖中的流程:

  1. 只要你點擊add按鈕那裏叫2選擇器(可能是一個其他內部或兩個IBAction爲實現一個鍵),它包含在其中
  2. 警報視圖代碼現在,警報2(與取消按鈕)獲得報警1之前調用(與OK鍵)
  3. 然後報警1被調用,並隱藏警報2
  4. 現在,當您解決報警1(點擊OK按鈕)警報2再次顯示了

現在你需要做的是檢查「如果你的按鈕沒有連接2 IBActions」,這應該是因爲你沒有這樣的代碼來調用另一個警報在這種方法。並檢查是否有幫助。

+0

不,我的「添加」按鈕只與addButtonPressed連接... – Bhavin

+0

你可以上傳,並提供一個鏈接。所以我可以檢查問題 – rptwsthi

+0

只需檢查UPDATE在我的問題中... – Bhavin

0
- (IBAction)addButtonPressed:(id)sender 
{ 
    if ([textField.text length]==0) 
    { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Information",@"") message:NSLocalizedString(@"Txt is Empty!",@"") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil]; 
     [alert show]; alert = nil; 
    } 
    else 
    { 
     // Code to add textField's value to Array. 
    } 
} 

首先檢查您點擊Button時撥打IBAction方法的次數是多少?的UIAlertView

其他明智的認沽實例是公共 ..我的意思放在.h file和訪問它作爲.m fileself.yourAlertViewName

謝謝:)

0

嘗試比較這樣。

if([testBox.text isEqualToString:@""] 
{ 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                message:errorDesc 
                delegate:self 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 

    [alert show]; 
} 
+0

檢查評論我的問題,我已經試過這種方式... – Bhavin

1

是我面對同樣的問題,但我的情況是不同於你。

  1. 您應該嘗試[textfield.text isEqualToString:@""];,因爲這是比較Objective-C中空白文本字段的標準方法。

  2. 檢查您關閉您的警報視圖正確有時我們不給予重點駁回警報視圖如此,是因爲你的警報視圖保持活躍,當你重新打開你的應用程式就說明根據2〜3倍的條件。因此,您可以使用代理確實關閉了帶有按鈕索引的警報視圖,用於解除視圖中的警報視圖。我不知道,但它應該爲你工作祝你好運夥計。

  3. 我不確定,但我認爲每次單擊任何按鈕時都會覆蓋您的IBAction按鈕,因此您應該檢查它。

+0

只是檢查更新在我的問題... – Bhavin

+0

+1爲您的第一點。 – Bhavin