2014-03-06 30 views
0

我是新來使用塊語法並面臨以下問題。下面的代碼調用導致問題的類的另一個靜態方法。下面的代碼被點擊下一個按鈕在欄上調用。這段代碼的語法有沒有錯誤?使用塊時選擇器沒有已知的類方法

-(BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender 
    { 
     if (![sender isKindOfClass:[UIBarButtonItem class] ]) { 
      return true; 
     } 
     // Trim the spaces 
     self.stewardsNameTextField.text = [self.stewardsNameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ]; 

    self.trackNameTextField.text = [self.trackNameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ]; 

    self.curatorNameTextField.text = [self.curatorNameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ]; 


    bool isValid =[JLTValidator validateFields: @[self.stewardsNameTextField, self.trackNameTextField, self.curatorNameTextField, self.weatherConditionSegment, self.trackConditionSegment] withScrollToCallback: ^(UIView * invalidField) // problem is here. Is this incorrect syntax? 
      { 
       if (invalidField == self.stewardsNameTextField || invalidField == self.trackNameTextField || invalidField == self.curatorNameTextField) 
       { 
        [invalidField becomeFirstResponder]; 
       } 
       else 
       { 
        UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
        CGPoint top = CGPointMake(0, invalidField.frame.origin.y - 90); 

        [_scrollView setContentOffset:top animated:YES]; 
        _scrollView.scrollIndicatorInsets=contentInsets; 
       } 

       if (!isValid) { 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:@"Please fill out the marked fields." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
        [alert show]; 
       } 

       return isValid; 
      }]; 

} 

靜態方法的定義被稱爲對類:

+(BOOL)validateFields:(NSArray *)fields 
    { 
     return [JLTValidator validateFields:fields withScrollToCallback:nil]; 
    } 
    +(BOOL)validateFields:(NSArray *)fields andShouldDisplayMessage : (bool) shouldDisplayMessage 
    { 
     return [JLTValidator validateFields:fields withScrollToCallback:nil andShouldDisplayMessage:shouldDisplayMessage]; 
    } 

+(BOOL)validateFields:(NSArray *)fields withScrollToCallback : (void (^) (UIView *))scrollToCallback 
{ 
    return [JLTValidator validateFields:fields withScrollToCallback:scrollToCallback andShouldDisplayMessage:true]; 
} 

+(BOOL)validateFields:(NSArray *)fields withScrollToCallback : (void (^) (UIView *))scrollToCallback andShouldDisplayMessage : (bool) shouldDisplayMessage 
{ 

} 

請告訴我錯在這裏?請指導。

+0

什麼是你想實現與回調?它應該返回一個BOOL嗎? – Wain

+0

wain ..是的,你是對的。它返回一個布爾值。 – LUI

回答

0
+(BOOL)validateFields:(NSArray *)fields withScrollToCallback : (void (^) (UIView *))scrollToCallback 

此方法中的塊參數不返回任何內容(您可以在其中看到'void')。所以如果在塊內,你返回BOOL值(isValid)是不正確的。而且,你返回了你試圖獲得的值(isValid = ..... isValid),這也是一個錯誤。

我不知道你就像塊裏面很努力,但要解決這個問題,你可以象下面這樣:

bool isValid =[JLTValidator validateFields: @[self.stewardsNameTextField, self.trackNameTextField, self.curatorNameTextField, self.weatherConditionSegment, self.trackConditionSegment] withScrollToCallback: ^(UIView * invalidField) // problem is here. Is this incorrect syntax? 
     { 
      if (invalidField == self.stewardsNameTextField || invalidField == self.trackNameTextField || invalidField == self.curatorNameTextField) 
      { 
       [invalidField becomeFirstResponder]; 
      } 
      else 
      { 
       UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
       CGPoint top = CGPointMake(0, invalidField.frame.origin.y - 90); 

       [_scrollView setContentOffset:top animated:YES]; 
       _scrollView.scrollIndicatorInsets=contentInsets; 
      } 
     }]; 

if (!isValid) { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:@"Please fill out the marked fields." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
    [alert show]; 
} 
相關問題