我是新手使用块语法并面临以下问题。下面的代码调用导致问题的类的另一个静态方法。通过点击栏上的下一个按钮调用下面的代码。这段代码的语法有什么错误吗?
-(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 :(得分:0)
+(BOOL)validateFields:(NSArray *)fields withScrollToCallback : (void (^) (UIView *))scrollToCallback
此方法中的block参数不返回任何内容(您可以在那里看到'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];
}