2014-09-29 25 views
0

我已初始化兩個UITextField s,tftf1。當視圖加載時,我希望兩個字段的文本分別爲_minPrice_maxPrice。這些是從前一個ViewController獲取它們值的兩個NSString屬性。當目前的ViewController加載時,它顯示正確的值,所以我知道這部分工作正常。UITextField未在函數中註冊任何文本長度

我希望用戶能夠編輯的tftf1的內容,這樣,無論是包含在這兩個UITextField用作以下editMatchCenter函數的參數,不管是從_minPrice_maxPrice的默認字符串,或用戶輸入的任何文本來代替。編輯字段工作正常,但當我按提交按鈕來運行editMatchCenter,沒有任何反應,這告訴我,self.tf.textself.tf1.text由於某種原因是空的。

我試着註釋掉if語句,我得到一個崩潰指出:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'

是指@"minPrice": self.tf.text

文本字段初始化:

 // Min Price 
     UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(80, 125, 70, 30)]; 
     [self.view addSubview:tf]; 

     tf.userInteractionEnabled = YES; 
     tf.textColor = [UIColor blackColor]; 
     tf.font = [UIFont fontWithName:@"Helvetica-Neue" size:14]; 
     tf.backgroundColor=[UIColor whiteColor]; 
     tf.text = _minPrice; 

     // Max Price 
     UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(195, 125, 70, 30)]; 
     [self.view addSubview:tf1]; 

     tf1.userInteractionEnabled = YES; 
     tf1.textColor = [UIColor blackColor]; 
     tf1.font = [UIFont fontWithName:@"Helvetica-Neue" size:14]; 
     tf1.backgroundColor=[UIColor whiteColor]; 
     tf1.text = _maxPrice; 

Submit按鈕功能:從頭

- (IBAction)submitButton:(id)sender 
    { 
     if (self.tf.text.length > 0 && self.tf1.text.length > 0) { 

      [PFCloud callFunctionInBackground:@"editMatchCenter" 
           withParameters:@{ 
               @"searchTerm": _searchTerm, 
               @"minPrice": self.tf.text, 
               @"maxPrice": self.tf1.text, 
               @"itemCondition": _itemCondition, 
               @"itemLocation": _itemLocation 
               } 
             block:^(NSString *result, NSError *error) { 

              if (!error) { 
               NSLog(@"Result: '%@'", result); 
               [self dismissViewControllerAnimated:YES completion:nil]; 
              } 
             }]; 
     } 
    } 

屬性:

@property (strong, nonatomic) NSString *minPrice; 
    @property (strong, nonatomic) NSString *maxPrice; 
    @property (strong, nonatomic) NSString *itemCondition; 
    @property (strong, nonatomic) NSString *itemLocation; 
    @property (strong, nonatomic) NSString *searchTerm; 

    @property (strong, nonatomic) IBOutlet UITextField *tf; 
    @property (strong, nonatomic) IBOutlet UITextField *tf1; 

回答

1

你得到零的原因是因爲你從未初始化過你的屬性。相反,你可以在代碼中創建新的指針(tf和tf1)。

嘗試用self.tf1替換tf與self.tf和tf1。像這樣:

self.tf = [[UITextField alloc] initWithFrame:CGRectMake(80, 125, 70, 30)]; 

self.tf1 = [[UITextField alloc] initWithFrame:CGRectMake(195, 125, 70, 30)];