0

I have used VENTokenField github library for recipients email. Now the user enter the email and its separated by commas(,). Everything works fine. My question is how to clear the text when user press the submit button.

1) https://i.stack.imgur.com/faf7F.jpg

2) https://i.stack.imgur.com/v6XSJ.jpg

@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 - To invitation

-(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];
        }
    }
}
Sakthi
  • 71
  • 1
  • 8

1 Answers1

0

In ViewController class,

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

In View class

Invite Button 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 setDelegate:self];
        [self.inviteVC sendInviationAPI:INVITE_FRIENDS withParameter:sendData];

    }
}

-(void)removeArray {

    [self.inviteFriendNames removeAllObjects];
    [self.tokenField reloadData];

}
Maniganda saravanan
  • 2,188
  • 1
  • 19
  • 35