如何清除 VENToken 中的文本?
Posted
技术标签:
【中文标题】如何清除 VENToken 中的文本?【英文标题】:How to clear text inside VENToken? 【发布时间】:2016-10-24 11:30:05 【问题描述】:我使用VENTokenField
github 库作为收件人电子邮件。现在用户输入电子邮件,并用逗号(,)分隔。一切正常。我的问题是当用户按下提交按钮时如何清除文本。
1)http://imgur.com/a/ObbaB
2)http://imgur.com/a/DCusg
@property (strong, nonatomic) NSMutableArray *inviteFriendNames;
self.inviteFriendNames = [[NSMutableArray alloc]init];
#pragma mark - VENTokenFieldDataSource
- (NSString *)tokenField:(VENTokenField *)tokenField titleForTokenAtIndex:(NSUInteger)index
if (self.inviteFriendNames.count > index)
return self.inviteFriendNames[index];
return @"";
- (NSUInteger)numberOfTokensInTokenField:(VENTokenField *)tokenField
return self.inviteFriendNames.count;
#pragma mark - VENTokenFieldDelegate
- (void)tokenField:(VENTokenField *)tokenField didEnterText:(NSString *)text
if (![self.inviteFriendNames containsObject:text])
self.inviteFriendNames = (NSMutableArray*)[self.inviteFriendNames arrayByAddingObject:text];
[self chosenContactsHasChanged];
return;
- (void)tokenField:(VENTokenField *)tokenField didDeleteTokenAtIndex:(NSUInteger)index
if (self.inviteFriendNames.count > index)
NSMutableArray *mutable = [self.inviteFriendNames mutableCopy];
[mutable removeObjectAtIndex:index];
self.inviteFriendNames = mutable;
[self chosenContactsHasChanged];
self.tokenField.placeholderText = NSLocalizedString(@"Email", nil);
[self chosenContactsHasChanged];
- (void)tokenField:(VENTokenField *)tokenField didChangeText:(NSString *)text
if ([text hasSuffix:@" "])
// Trim off the comma
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]];
[self tokenField:tokenField didEnterText:text];
else
return;
- (void)updateContactTokenFieldHeightConstraint
CGFloat heightOfTokens = self.tokenField.tokenScrollViewHeight + tokenFieldHeightPadding;
CGFloat newHeight = MAX(tokenFieldMinHeight, heightOfTokens);
self.tokenFieldHeight.constant = newHeight;
[self layoutIfNeeded];
- (void)chosenContactsHasChanged
[self.tokenField reloadData];
[self updateContactTokenFieldHeightConstraint];
pragma mark - UIButton Action
-(IBAction)inviteButtonAction:(id)sender
[self endEditing:YES];
if (self.inviteFriendNames.count == 0)
[[PCAlertUtility sharedInstance]showOkAlertWithTitle:nil description:@"Please enter the mail id." completion:nil];
else
NSString *senderName = [NSString stringWithFormat:@"%@.%@",[APPDELEGATE.defaults valueForKey:@"firstName"],[APPDELEGATE.defaults valueForKey:@"lastName"]];
NSDictionary *sendData = @@"name":senderName,@"emails":inviteFriendNames;
[self.inviteVC sendInviationAPI:INVITE_FRIENDS withParameter:sendData];
pragma mark - 到邀请
-(void)sendInviationAPI:(NSString *)url withParameter:(NSDictionary *)parameters
// To Check Network Available or Unavailable
if (![PCCommonUtility checkForNetwork])
[[PCAlertUtility sharedInstance]showOkAlertWithTitle:[LocalizedStrings warning] description:[LocalizedStrings networkFailure] completion:nil];
else
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
// To Check Access token is null or empty
if ([APPDELEGATE.defaults valueForKey:CURRENT_TOKEN] && ![[APPDELEGATE.defaults valueForKey:CURRENT_TOKEN]isKindOfClass:[NSNull class]])
[PCAPIUtility getResponseFromAPI:url withParameters:parameters withSuccess:^(NSMutableArray *responseData, NSString *success)
DLog(@"InviteSuccess:%@",responseData);
NSString *status = [NSString stringWithFormat:@"%@",[responseData valueForKey:@"status"]];
NSString *message = [NSString stringWithFormat:@"%@",[responseData valueForKey:@"message"]]
if ([status isEqualToString:@"1"])
[MBProgressHUD hideHUDForView:self.view animated:YES];
[[PCAlertUtility sharedInstance]showOkAlertWithTitle:nil description:message completion:^(BOOL confirm)
if (!confirm)
[self.inviteView.inviteFriendNames removeAllObjects];
[self.inviteView.tokenField reloadData];
];
else
[MBProgressHUD hideHUDForView:self.view animated:YES];
[[PCAlertUtility sharedInstance]showOkAlertWithTitle:nil description:message completion:nil];
orFailure:^(NSError *error)
DLog(@"InviteFailure:%@",error.description);
[MBProgressHUD hideHUDForView:self.view animated:YES];
[[PCAlertUtility sharedInstance]showOkAlertWithTitle:nil description:[LocalizedStrings networkFailure] completion:nil];
];
else
NSLog(@"Access Token is Empty");
[MBProgressHUD hideHUDForView:self.view animated:YES];
【问题讨论】:
inputTextField.text = @""; github.com/venmo/VENTokenField/tree/master/VENTokenField你正在使用这个库,所以当你警告显示和它的显示明文时只写一行。 这个我也试了,但是不行 试试我的答案并告诉我它是否有效? 【参考方案1】:在 ViewController 类中,
.h
@protocol DelegateMethod <NSObject>
-(void)removeArray;
@end
@interface ViewController : UIViewController
@property (nonatomic, strong) id <DelegateMethod> delegate;
@end
.m
-(void)sendInviationAPI:(NSString *)url withParameter:(NSDictionary *)parameters
// To Check Network Available or Unavailable
if (![PCCommonUtility checkForNetwork])
[[PCAlertUtility sharedInstance]showOkAlertWithTitle:[LocalizedStrings warning] description:[LocalizedStrings networkFailure] completion:nil];
else
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
// To Check Access token is null or empty
if ([APPDELEGATE.defaults valueForKey:CURRENT_TOKEN] && ![[APPDELEGATE.defaults valueForKey:CURRENT_TOKEN]isKindOfClass:[NSNull class]])
[PCAPIUtility getResponseFromAPI:url withParameters:parameters withSuccess:^(NSMutableArray *responseData, NSString *success)
DLog(@"InviteSuccess:%@",responseData);
NSString *status = [NSString stringWithFormat:@"%@",[responseData valueForKey:@"status"]];
NSString *message = [NSString stringWithFormat:@"%@",[responseData valueForKey:@"message"]]
if ([status isEqualToString:@"1"])
[MBProgressHUD hideHUDForView:self.view animated:YES];
[[PCAlertUtility sharedInstance]showOkAlertWithTitle:nil description:message completion:^(BOOL confirm)
if (!confirm)
[self.delegate removeArray];
];
else
[MBProgressHUD hideHUDForView:self.view animated:YES];
[[PCAlertUtility sharedInstance]showOkAlertWithTitle:nil description:message completion:nil];
orFailure:^(NSError *error)
DLog(@"InviteFailure:%@",error.description);
[MBProgressHUD hideHUDForView:self.view animated:YES];
[[PCAlertUtility sharedInstance]showOkAlertWithTitle:nil description:[LocalizedStrings networkFailure] completion:nil];
];
else
NSLog(@"Access Token is Empty");
[MBProgressHUD hideHUDForView:self.view animated:YES];
在视图类中
邀请按钮操作
-(IBAction)inviteButtonAction:(id)sender
[self endEditing:YES];
if (self.inviteFriendNames.count == 0)
[[PCAlertUtility sharedInstance]showOkAlertWithTitle:nil description:@"Please enter the mail id." completion:nil];
else
NSString *senderName = [NSString stringWithFormat:@"%@.%@",[APPDELEGATE.defaults valueForKey:@"firstName"],[APPDELEGATE.defaults valueForKey:@"lastName"]];
NSDictionary *sendData = @@"name":senderName,@"emails":inviteFriendNames;
[self.inviteVC setDelegate:self];
[self.inviteVC sendInviationAPI:INVITE_FRIENDS withParameter:sendData];
-(void)removeArray
[self.inviteFriendNames removeAllObjects];
[self.tokenField reloadData];
【讨论】:
我从数组中删除所有对象时遇到异常 'removeAllObjects' 在调用邀请 API 服务后。不要一开始就删除并将一个空数组发送到您的服务器。 我将值发送到 API 并在我 removeAllObjects 之后得到成功的响应 您仍然面临问题吗? 是的,我仍然面临这个问题,我已经更新了我的代码。请检查以上是关于如何清除 VENToken 中的文本?的主要内容,如果未能解决你的问题,请参考以下文章
调用popToViewController后如何清除视图中的文本框