我想在我的textview上倒計時。我的.h和.m文件安裝如下。基本上,我所需要的是一個textview標籤,當用戶輸入文本時,該標籤從140開始倒數。一旦textview達到140個字符,用戶應該不能再輸入文字。我不確定我在這裏做錯了什麼。任何幫助將是偉大的!謝謝!Textview倒數標籤
@interface LeaveTipViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIBarButtonItem *tipCancelButton;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *tipSubmitButton;
@property (weak, nonatomic) IBOutlet UITextView *tipText;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
- (IBAction)tipCancelPressed:(id)sender;
- (IBAction)tipSubmitPressed:(id)sender;
@end
在我的.m文件:
在我.h文件中
-(void)textViewDidChange:(UITextView *)textView
{
int len = tipText.text.length;
countLabel.text=[NSString stringWithFormat:@"%i",140-len];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
BOOL flag = NO;
if([text length] == 0)
{
if([tipText.text length] != 0)
{
flag = YES;
return YES;
}
else {
return NO;
}
}
else if([[tipText text] length] > 139)
{
return NO;
}
return YES;
}
嗨,我遇到了麻煩self.tipText.delegate = self ...我在哪裏放置那部分? – Brandon
In viewDidLoad方法。或者在Interface Builder中,只需將代理字段指向您的類 – nerowolfe
woohoo!確定它的工作原理,但我得到一個錯誤:分配給ID從不兼容的類型leavetipcontroller const_strong –
Brandon