【问题标题】:How to clear text inside VENToken?如何清除 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];
        }
    }
}

【问题讨论】:

标签: ios objective-c iphone uitextview uitextviewdelegate


【解决方案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 之后得到成功的响应
  • 您仍然面临问题吗?
  • 是的,我仍然面临这个问题,我已经更新了我的代码。请检查
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
  • 1970-01-01
  • 2018-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多