Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7830819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:25:00+00:00 2026-06-02T11:25:00+00:00

I have two view controllers. The CardWallet View Controller is my table view. Then

  • 0

I have two view controllers. The CardWallet View Controller is my table view. Then the AddCard View Controller is where I input values for a new instance of an object named Card. So far, I am adding those Card instances in an array named myWallet which is in my CardWallet View Controller using a delegate and it works.

What I want is, after clicking the button in my AddCard View Controller, a new table cell will appear in my Card Wallet View, with the name depending on the recently added instance of Card. Below is my code, kindly check why is it that when I’m finished adding a new instance of Card, nothing appears in my table. I’ve done some research and went through some tutorials, this one is good, http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-and.html, it helped me a lot regarding table view controllers. However, the tutorial doesn’t cater my main concern for it’s table’s values only come from an array with static values.

Thanks!

CardWalletViewController.h

#import <UIKit/UIKit.h>

@interface CardWalletViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

}

@property (nonatomic, strong) NSMutableArray *myWallet;

-(void) printArrayContents;
@end

CardWalletViewController.m

#import "CardWalletViewController.h"
#import "AddCardViewController.h"
#import "Card.h"

@interface CardWalletViewController () <AddCardDelegate>

@end

@implementation CardWalletViewController


@synthesize myWallet = _myWallet;

- (NSMutableArray *) myWallet
{
    if (_myWallet == nil) _myWallet = [[NSMutableArray alloc] init]; 
    return _myWallet;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showAddCardVC"]) {
        AddCardViewController *addCardVC = (AddCardViewController *)segue.destinationViewController;

        addCardVC.delegate = self;

    }
}

- (void)printArrayContents 
{
    // I want to show the name of each instance of card

    for ( int i = 0; i < self.myWallet.count; i++) {
        Card *cardDummy = [self.myWallet objectAtIndex:i];
        NSLog(@"Element %i is %@", i,cardDummy.name );
    }

}

- (void)addCardViewController:(AddCardViewController *)sender didCreateCard:(Card *)newCard
{
    // insert a new card to the array

    [self.myWallet addObject:newCard];

    [self printArrayContents];
}





- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
  // Release any retained subviews of the main view.
}

- (void)viewWillAppear:(BOOL)animated 
{

}

- (void)viewWillDisappear:(BOOL)animated
{

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    //this method will return the number of rows to be shown

    return self.myWallet.count;   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }
    // Configure the cell...

    //---------- CELL BACKGROUND IMAGE -----------------------------
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.frame];
    UIImage *image = [UIImage imageNamed:@"LightGrey.png"];
    imageView.image = image;
    cell.backgroundView = imageView;
    [[cell textLabel] setBackgroundColor:[UIColor clearColor]];
    [[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]]; 


    //this will show the name of the card instances stored in the array

    //

    for ( int i = 0; i < self.myWallet.count; i++) {
        Card *cardDummy = [self.myWallet objectAtIndex:i];
        cell.textLabel.text = cardDummy.name;
    }

    //Arrow 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

AddCardViewController.h

#import <UIKit/UIKit.h>
#import "Card.h"

@class AddCardViewController;

@protocol AddCardDelegate <NSObject>

- (void)addCardViewController:(AddCardViewController *)sender
                didCreateCard:(Card *) newCard;

@end


@interface AddCardViewController : UIViewController <UITextFieldDelegate>


@property (strong, nonatomic) IBOutlet UITextField *cardNameTextField;
@property (strong, nonatomic) IBOutlet UITextField *pinTextField;
@property (strong, nonatomic) IBOutlet UITextField *pointsTextField;

@property (nonatomic, strong) id <AddCardDelegate> delegate;

@end

AddCardViewController.m

#import "AddCardViewController.h"
#import "Card.h"
#import "CardWalletViewController.h"

@interface AddCardViewController ()

@end

@implementation AddCardViewController 

@synthesize cardNameTextField = _cardNameTextField;
@synthesize pinTextField = _pinTextField;
@synthesize pointsTextField = _pointsTextField;

@synthesize delegate = _delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.cardNameTextField becomeFirstResponder];
}

- (void) viewWillDisappear:(BOOL)animated
{

}

- (BOOL) textFieldShouldReturn:(UITextField *)textField{

    if ([textField.text length]) {
    [self.cardNameTextField resignFirstResponder];

    [self.pinTextField resignFirstResponder];

    [self.pointsTextField resignFirstResponder];

    return YES;
    }

    else {
        return NO;
    }
}

- (void)viewDidLoad
{
    self.cardNameTextField.delegate = self;
    self.pinTextField.delegate = self;
    self.pointsTextField.delegate = self;
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setCardNameTextField:nil];
    [self setPinTextField:nil];
    [self setPointsTextField:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)addCard:(id)sender 
{
    Card *myNewCard = [[Card alloc] init];

    myNewCard.name = self.cardNameTextField.text;

    myNewCard.pin = self.pinTextField.text;

    myNewCard.points = [self.pointsTextField.text intValue];

    // to check if the text fields were filled up by the user
    if ([self.cardNameTextField.text length] && [self.pinTextField.text length] && [self.pointsTextField.text length]) 
    {

        [[self presentingViewController] dismissModalViewControllerAnimated:YES];

        NSLog(@"name saved %@", myNewCard.name);
        NSLog(@"pin saved %@", myNewCard.pin);
        NSLog(@"points saved %i", myNewCard.points);

        [self.delegate addCardViewController:self didCreateCard:myNewCard];

        // to check if there is a delegate
         if (self.delegate){
            NSLog(@"delegate is not nil");
        }

    }
}

@end

Card.h

#import <Foundation/Foundation.h>

@interface Card : NSObject

    @property (nonatomic, strong) NSString *name;
    @property (nonatomic, strong) NSString *pin;
    @property (nonatomic) int points;

@end

Card.m

#import "Card.h"

@implementation Card

@synthesize name = _name;
@synthesize pin = _pin;
@synthesize points = _points;

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:25:01+00:00Added an answer on June 2, 2026 at 11:25 am

    I should get the obvious question out of the way before anyone starts dwelling too deep into this – do you have some mechanism of reloading the data after you add a new card (e.g. call [tableView reloadData] from the CardWalletViewController)? I didn’t see anything like that, and I’ve always used this whenever I add something new to a table.*

    *If the table contains too much data, you may want to reload only a part of it.

    Update 1: Class Inheritance

    Every Objective C class has to inherit from some other class in the hierarchy. By default, unless you say otherwise, all of your custom classes will inherit from NSObject, which is the most generic object out there (equivalent of Object, if you’ve done Java programming). Changing the parent class is done by simply changing the class after the : in your interface declaration. So when you say
    @interface CardWalletViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
    what you are saying is “declare a CardWallerViewController custom class that inherits from UIViewController and implements the UITableViewDelegate and UITableViewDataSource protocols” (if you don’t know what protocols are, ask).

    Now, back to your question. Changing the parent class should be easy now – you just change that : UIViewController to : UITableViewController and you are done. After you do this, your CardWallerViewController (also, “Waller”, really?) will behave like a UITableView, not like a generic UIView. When doing this, you will also not need to tell it to implement the delegate and dataSource protocols – UITableViewController does that by default.

    As a final note, when you add new files to your Xcode project, you can tell the program which class you want to inherit from. It defaults to UIView for views, but that’s simply because this is the most generic view class. As you begin to use more specific classes (UITableViewController, UIPickerViewController, UITableViewCell, to name a few), changing the parent class off the bat will prove to be more than helpful.

    Update 2: UITableViewCells

    That for-loop you’ve got going there is a (relatively) lot of work you don’t need to do. Since your table corresponds directly to your myWallet property, this means that the cell in row N of your table will represent the card at index N of your array. You can use that to your advantage. In the tableView:cellForRowAtIndexPath: method, you tell the program what to do with the cell at the specific indexPath (which is really just section + row for that table). The trick is, this method instantiates the cells one at a time. So instead of doing the for-loop, you can just say (at the end)
    cell.textLabel.text = [self.myWallet objectAtIndex:indextPath.row].name;

    For any cell in row N, this will look at the Nth Card object inside myWallet and use its name to set the cell’s textLabel.text. If it gives you problems, save [self.myWallet objectAtIndex:indextPath.row] in some tempCard object, and then do cell.textLabel.text = tempCard.name. This is also the proper way to populate cells in a tableView – you only care about one cell at a time, because that’s how the method works anyway. Imagine if you had 1,000,000 Cards inside your array – doing the for-loop would force the program to go through the array 1,000,000 times for each cell. Say hello to a 1,000,000,000,000 operations 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two View Controllers: TableViewController (which is used as a modal view controller)
In my app i have two view controllers and have table view in both.Both
I have two table view controllers. How can I make shadow like this?
The Problem: I have two View Controllers loaded into a root View Controller. Both
I have two view controllers, in which I am using navigation controller to navigate
I have a view controller that initializes two other view controllers. The view for
I have two view controllers named view1ViewController and view2ViewController in an cocoa touch project,
I have two table view controllers. Say TableViewController1 and TableViewController2 . I push TableViewController2
I have two view controllers in a tabbar which can both edit data. Therefore,
I have two view controllers that allow changes to the Address Book. The first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.