如何清除VENToken中的文字?

时间:2016-10-24 11:30:06

标签: ios objective-c iphone uitextview uitextviewdelegate

我已将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];
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在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];
        }
    }
}

在View类中

邀请按钮操作

-(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];

}