Saya telah membuat array di controller saya. Berdasarkan array JSON itu, kartu saya[tinderswipecards/cwRichard] dibuat. Sekarang saya ingin jika saya mengeluarkan kartu, saya ingin mendapatkan data kartu yang Ditampilkan untuk ditampilkan di suatu tempat di viewController saya seperti iniseperti ini

Dan string yang akan dibuat seperti

 int i;
 for (i = 0; i < [_peopleNearBypeopleList count]; i++) {


  PeopleNearByIndividualModel * model = [_peopleNearBypeopleList objectAtIndex:i];




  NSString *nameString = @"ARE YOU SURE YOU WANT TO INVITE ";



  nameString = [nameString stringByAppendingString:model.displayName];
    NSLog(@"%@", nameString);

    _textView.textColor = [UIColor whiteColor];
    _textView.font = [UIFont fontWithName:@"Roboto-Regular" size:17.0f];
    _textView.text = nameString;

}

Lagi dan itu memanggil Objek pertama. bagaimana saya bisa mencapai ini?

Ini adalah Kelas ViewBackground saya yang dapat diseret

      //%%% loads all the cards and puts the first x in the "loaded cards" array
     -(void)loadCards:(NSArray*)array
     {

     exampleCardLabels = array;


     if([exampleCardLabels count] > 0) {
     NSInteger numLoadedCardsCap =(([exampleCardLabels count] > MAX_BUFFER_SIZE)?MAX_BUFFER_SIZE:[exampleCardLabels count]);
     //%%% if the buffer size is greater than the data size, there will be an array error, so this makes sure that doesn't happen



       //%%% loops through the exampleCardsLabels array to create a card for each label.  This should be customized by removing "exampleCardLabels" with your own array of data
          for (int i = 0; i<[exampleCardLabels count]; i++) {
              MG_DraggableVIew* newCard = [self  createDraggableViewWithDataAtIndex:i];
            [allCards addObject:newCard];

            if (i<numLoadedCardsCap) {
            //%%% adds a small number of cards to be loaded
            [loadedCards addObject:newCard];
        }
    }



           //%%% displays the small number of loaded cards dictated by MAX_BUFFER_SIZE so that not all the cards
          // are showing at once and clogging a ton of data
           for (int i = 0; i<[loadedCards count]; i++) {
            if (i>0) {
            [self insertSubview:[loadedCards objectAtIndex:i] belowSubview:    [loadedCards objectAtIndex:i-1]];
        } else {
            [self addSubview:[loadedCards objectAtIndex:i]];
        }
         cardsLoadedIndex++; //%%% we loaded a card into loaded cards, so we have to increment
         }
      } 
//      }
     }

    #warning include own action here!
   //%%% action called when the card goes to the left.
   // This should be customized with your own action
   -(void)cardSwipedLeft:(UIView *)card;
  {
//do whatever you want with the card that was swiped
//    DraggableView *c = (DraggableView *)card;

     [loadedCards removeObjectAtIndex:0]; //%%% card was swiped, so it's no  longer a "loaded card"

    if (cardsLoadedIndex < [allCards count]) { //%%% if we haven't reached the end of all cards, put another into the loaded cards
    [loadedCards addObject:[allCards objectAtIndex:cardsLoadedIndex]];
    cardsLoadedIndex++;//%%% loaded a card, so have to increment count
    [self insertSubview:[loadedCards objectAtIndex:(MAX_BUFFER_SIZE-1)] belowSubview:[loadedCards objectAtIndex:(MAX_BUFFER_SIZE-2)]];
}





          }

         #warning include own action here!
      //%%% action called when the card goes to the right.
      // This should be customized with your own action
      -(void)cardSwipedRight:(UIView *)card
     {
     //do whatever you want with the card that was swiped
      //    DraggableView *c = (DraggableView *)card;

        [loadedCards removeObjectAtIndex:0]; //%%% card was swiped, so it's no longer a "loaded card"

        if (cardsLoadedIndex < [allCards count]) { //%%% if we haven't reached the end of all cards, put another into the loaded cards
    [loadedCards addObject:[allCards objectAtIndex:cardsLoadedIndex]];
    cardsLoadedIndex++;//%%% loaded a card, so have to increment count
    [self insertSubview:[loadedCards objectAtIndex:(MAX_BUFFER_SIZE-1)] belowSubview:[loadedCards objectAtIndex:(MAX_BUFFER_SIZE-2)]];
}







           }

        //%%% when you hit the right button, this is called and substitutes the swipe
        -(void)swipeRight
     {
 MG_DraggableVIew *dragView = [loadedCards firstObject];
 dragView.overlayView.mode = GGOverlayViewModeRight;
 [UIView animateWithDuration:0.2 animations:^{
     dragView.overlayView.alpha = 1;
 }];
 [dragView rightClickAction];



  }

       //%%% when you hit the left button, this is called and substitutes the swipe
     -(void)swipeLeft
    {
  MG_DraggableVIew *dragView = [loadedCards firstObject];
  dragView.overlayView.mode = GGOverlayViewModeLeft;
 [UIView animateWithDuration:0.2 animations:^{
    dragView.overlayView.alpha = 1;
  }];
   [dragView leftClickAction];
   }
1
Nivesh 7 Desember 2016, 13:25

2 jawaban

Jawaban Terbaik

Anda dapat memposting NSNotification di dalam -(void)cardSwipedLeft:(UIView *)card & -(void)cardSwipedRight:(UIView *)card metode di DraggableViewBackground.

NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:index] forKey:@"someKey"];

[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:self userInfo:userInfo];

Di sini indeks adalah indeks kartu Anda. Untuk mengatur indeks, Anda dapat mendeklarasikan properti int bernama index. Tetapkan nilai nilai awal indeks seperti index=0; dalam metode initwithframe.

Kemudian, tingkatkan nilai indeks saat Anda menggesek kartu.

Set index++; di dalam -(void)cardSwipedLeft:(UIView *)card & -(void)cardSwipedRight:(UIView *)card metode di DraggableViewBackground.

Dengarkan pemberitahuan ini di pengontrol tampilan Anda. Untuk mencapai itu tambahkan metode pengontrol tampilan di viewDidLoad dengan baris ini:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receiveTestNotification:)
                                                 name:@"TestNotification"
                                               object:nil];

Terapkan metode pemilih ini di viewcontroller- Anda

- (void) receiveTestNotification:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:@"TestNotification"]){
         NSLog (@"Successfully received the test notification!");
        NSDictionary *userInfo = notification.userInfo;
        int myObject = [[userInfo objectForKey:@"someKey"]integerValue];
        NSLog(@"index: %d", myObject);
    }

}

Pada metode receiveTestNotification Anda mendapatkan indeks kartu. maka Anda bisa mendapatkan data dari array Anda dengan index.

1
Dipankar Das 7 Desember 2016, 13:05

Menurut pendapat saya, akan lebih baik bagi Anda jika Anda mempertahankan dictionary untuk kartu dan data mereka.

Anda dapat membuat kamus dengan kunci sebagai id kartu/nomor kartu dan nilai sebagai datanya (deskripsi.)

Jadi, setiap kali Anda mengeluarkan kartu, cukup ambil data dari dictionary.

Katakanlah cardsDictionary adalah dictionary Anda seperti ini :

cardsDictionary = @{@"card1_id":@"card1_data"......@"cardn_id":@"cardn_data"};

Kemudian saat mengeluarkan kartu, Anda dapat melakukan seperti ini:

card_data = cardsDictionary[@"card_id"];
0
Vishal Sonawane 7 Desember 2016, 11:09