2016-12-07 130 views
0

在我的iOS應用程序中,我有一個用戶界面有兩件事:一個選擇器視圖和一個按鈕。添加NSString到NSMutableArray崩潰

我初始化我選擇器視圖用的NSMutableArray這樣的:

@interface ViewController() 
{ 
    NSMutableArray *_pickerDataCategory; 
} 
- (void)viewDidLoad 
{ 
    // Initialize Data 
    _pickerDataCategory = [[NSMutableArray alloc] init]; 

    NSMutableArray *array = [NSMutableArray arrayWithObjects: 
          @"cat1", @"cat2", @"cat3", @"cat4", nil ]; 

    _pickerDataCategory = array; 

//First I had initialize like this : NSMutableArray *_pickerDataCategory = [NSMutableArray arrayWithObjects: 
          @"cat1", @"cat2", @"cat3", @"cat4", nil ]; 
} 

然後,我有一個按鈕,顯示彈出,用戶可以寫的東西。這種方法的目的是爲添加一個新的NSString對象到我的NSMutableArray

- (IBAction)addNewCategory:(id)sender { 
     __block bool isNotExist = false; 
      UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Add new category" 
                        message: @"Please write a new category" 
                      preferredStyle:UIAlertControllerStyleAlert]; 
     [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
      textField.placeholder = @"Category"; 
      textField.textColor = [UIColor blueColor]; 
      textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
      textField.borderStyle = UITextBorderStyleRoundedRect; 
     }]; 
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
     [alertController dismissViewControllerAnimated:YES completion:nil]; 
    }]; 

    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
     NSString *input = alertController.textFields[0].text; 
     //Check if the category exist already 

     for (NSString *category in _pickerDataCategory) 
     { 
      if ([category isEqualToString:input]) 
      { 
       [self popupMessage:@"Category already exists" title:@"Error"]; 
       isNotExist = false; 
       return; 
      }else{ 
       NSString *msg = [@"Category has been added : " stringByAppendingString:input]; 
       [self popupMessage:msg title:@"Ok"]; 
       isNotExist = true; 


       //[_pickerDataCategory addObject:input];//CRASH 
       // [_picker_cateogry reloadAllComponents]; 

      } 
     } 



    }]; 

    [alertController addAction:cancel]; 
    [alertController addAction:ok]; 

    [self presentViewController:alertController animated:YES completion:nil]; 

} 

但它崩潰時,我想添加文本字段輸入到我的NSMutableArray。我真的不明白爲什麼。我很小心我的NSMutableArray的初始化。我在網上搜索了關於它的任何信息,我發現的兩個信息是關於初始化,或者它是可變數組而不是簡單數組。

此外,我不明白爲什麼如果我在[self presentViewController:alertController animated:YES completion:nil];後添加一些代碼,它不會被執行......我沒有找到任何有關它的信息。

你能幫我弄清楚它爲什麼崩潰,它不工作? 謝謝

+2

您能否添加您獲得的崩潰消息?會讓問題變得更容易一點! :) – Rikh

+0

@rikh,是的忘記了。我只是編輯我的帖子;)謝謝! – lilouch

+0

把一個斷點放在你試圖將'addObject'添加到數組並檢查值是否爲'nil'? – Rikh

回答

3

就你的崩潰而言,它可能與你在枚舉數組時嘗試修改數組有關。我會把這些分成兩個獨立的任務。

喜歡的東西...

NSMutableArray *additionalObjects = [[NSMutableArray alloc] init]; 

for (NSString *category in _pickerDataCategory) { 
    if (![category isEqualToString:input]) { 
     [additionalObjects addObject: category]; 
    } 
} 

[_pickerDataCategory addObjectsFromArray: additionalObjects]; 
+0

好弗蘭克好工作 – SM18

+0

謝謝!我只是試着像你說的那樣完成兩項任務。一旦用戶寫了一些東西,NSString被存儲,然後在循環之後,我將它添加到NSMutableArray。 你知道爲什麼,如果我在presentViewController之後放一些代碼,它不會被執行嗎? – lilouch

+0

喜歡什麼樣的代碼? – Frankie

3

的問題是,您要值添加到您的陣列,同時在迭代,節省其他陣列的數據,然後將其添加到您的_pickerDataCategory

+0

不錯的觀察Marat – SM18

+0

對!我沒有注意到,實際上... – lilouch

3

的代碼將如下所示: - 枚舉時

- (IBAction)addNewCategory:(id)sender { 
    __block bool isNotExist = false; 
     UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Add new category" 
                       message: @"Please write a new category" 
                     preferredStyle:UIAlertControllerStyleAlert]; 
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
     textField.placeholder = @"Category"; 
     textField.textColor = [UIColor blueColor]; 
     textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
     textField.borderStyle = UITextBorderStyleRoundedRect; 
    }]; 
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    [alertController dismissViewControllerAnimated:YES completion:nil]; 
}]; 

UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    NSString *input = alertController.textFields[0].text; 
    //Check if the category exist already 
    NSMutableArray * arrTemp = _pickerDataCategory; 
    for (NSString *category in arrTemp) 
    { 
     if ([category isEqualToString:input]) 
     { 
      [self popupMessage:@"Category already exists" title:@"Error"]; 
      isNotExist = false; 
      return; 
     }else{ 
      NSString *msg = [@"Category has been added : " stringByAppendingString:input]; 
      [self popupMessage:msg title:@"Ok"]; 
      isNotExist = true; 


      [_pickerDataCategory addObject:input];//CRASH 
      [_picker_cateogry reloadAllComponents]; 

     } 
    } 



}]; 

[alertController addAction:cancel]; 
[alertController addAction:ok]; 

[self presentViewController:alertController animated:YES completion:nil]; 

    } 
+0

只需要將您的枚舉數組替換爲temp – SM18