NSMutableArray Returns NULL - objective-c

I try adding objects to NSMutableArray from another class (secondViewController) and then add it to my UITableView in my FirstViewController, but it returns null when I print it using NSLog. Here is my set up.
FirstViewController.h:
#interface FirstViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>{
IBOutlet UITableView *mytableview;
NSMutableArray *mytableinfo;
}
#property (nonatomic,retain) IBOutlet UITableView *mytableview;
#property (retain) IBOutlet NSMutableArray *mytableinfo;
FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
#synthesize mytableinfo,mytableview;
-(IBAction)addShift:(id)sender{
SecondViewController *secondViewController = [[SecondViewController alloc]init];
[self presentModalViewController:secondViewController animated:YES];
}
- (void)viewDidLoad
{
mytableinfo = [[NSMutableArray alloc]init];
[super viewDidLoad];
}
SecondViewController.m
#import "SecondViewController.h"
#import "FirstViewController.h"
#implementation SecondViewController
#synthesize dateformatter,mydatepicker,startingTime;
-(IBAction)saveShift:(id)sender{
FirstViewController *firstViewController = [[FirstViewController alloc]init];
[firstViewController.mytableinfo addObject:#"Hello world"];
NSLog(#"%#",firstViewController.mytableinfo);
[self dismissModalViewControllerAnimated:YES];
}
My goal is to ultimately feed a mytableviewfrom mytableinfo. I'm not even sure if this is the best way to go about it. Any advice would be appreciated.

In SecondViewController, you are creating a FirstViewController with alloc init. At that point, mytableinfo on FirstViewController is nil because you don't allocate until viewDidLoad.
What loads SecondViewController? Because you're dismissing it modally. If it's FirstViewController, then when you alloc init first view controller, you're not calling the instance that presented it modally.
It's also not very MVC to have one view poke at another like that. It creates code that's couple at the view layer and modifying data at the view layer. It is better to create a model and have both views modifying that model.
How to create a NSMutable Array which can access from different view controllers
Another way to communicate between views is for one view to pass a delegate (a callback) to the other view. That allows the other view to not be coupled to the other view - it only knows about the protocol for the delegate.
What exactly does delegate do in xcode ios project?

There is a point that look strange to me in your "SecondViewController" you dissmiss it like it's a modal.
My Question is then... who started the modal presentation?
A "FirstViewController"? If it's the case, why are you creating a new one, on dismissing the second, the First that launched it will resume it's activity.
An other thing that I don't understand is that the designated initializer for a UIViewController is
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
You can pass nil to both argument, if no nib need to be associated with.
And finaly, if you need to get back a NSMutableArray to a 1st ViewController (VC) from a 2nd VC that was modally presented by the 1st you can do this in the 2nd VC:
- (id)initWithMutableArray:(NSMutableArray *)theArray {
//... put standard init code here }
And make that the default initializer of your second VC. But this make sense only if 2nd VC absolutely need a mutable array.
And now for my curiosity because I don't understand this line
#property (retain) IBOutlet NSMutableArray *mytableinfo;
Why is this an IBOutlet? That look like a potential source of problem.
IBOutlet are usually pointers to UI elements in a xib file.

When populating a UITableView with an array that can be modified by multiple modal views during the course of your app, I find one of the best ways to do this is with NSUserDefaults. You can create an NSUserDefaults object for reference like this:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
Then you can assign objects to each key in defaults, which is really just a plist (which is just a list of keys with objects associated with them.
So then, when you want to store the array in defaults, you can say:
[defaults setObject:mytableinfo forKey:#"tableInformationKey"];
Then, whenever you want to access that data, you can say:
NSMutableArray* tableInfoCopy = [defaults mutableArrayValueForKey:#"tableInformationKey"];
That will make you a copy of the array you have stored in NSUserDefaults (NSUserDefaults can be accessed from anywhere in your app), so then you can make changes to that mutable array you just made. Once you are done making changes, you can reassign it to NSUserDefaults like this:
[defaults setObject:tableInfoCopy forKey#"tableInformationKey"];
So when you populate your UITableView, in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
put something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Foobar"];
if (cell == nil) {
// No cell to reuse => create a new one
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Foobar"] autorelease];
// Initialize cell with some customization
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.textColor = [UIColor blackColor];
}
NSArray* arrayOne = [defaults objectForKey:#"tableInformationKey"];
NSString* title = [arrayTwo objectAtIndex:indexPath.row];
//this goes to the index in the array of whatever cell you are
// at, which will populate your table view with the contents of this array (assuming the array contains strings)
// Customize cell
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:25];
return cell;
}

Use them the easiest way is to put the array in your AppDelegate
// populate appDelegate's array
AppDelegate *appDelegate = (myAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDelegate.arrayMyTableInfo addObject:#"HelloWorld"];

Here is how you can do what I've suggested :
(This is part of your code updated)
This is in your secondView, this is one of many way to pass the array to your second view.
#synthesize array4Test;
- (id)initWithMutableArray:aMutableArray
{
self = [self initWithNibName:nil bundle:nil];
if (self)
{
self.array4Test = aMutableArray;
}
return self;
}
- (void)dealloc
{
// HERE clean up the property is set to retain.
self.array4Test = nil;
[super dealloc];
}
Here is the code for the firstView
-(IBAction)addShift:(id)sender{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithMutableArray:self.mytableinfo];
[self presentModalViewController:secondViewController animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.mytableview reloadData];
NSString *aString = [mytableinfo lastObject];
if (aString)
{
NSLog(#"This just came back from the second View\n%#", aString);
}
}

Related

Not able to obtain correct indexPath.row value from objective C file in Swift file

File: ContactsViewController.m
In this file I am using the didSelectRowAtIndexPath method to push a new View Controller to show information about the name that was pressed on the Table View Controller. The View Controller that will be displaying the information about the name is being implemented in Swift. The part that I am referring to in this code is in the didSelectRowAtIndexPath method, line:
_myIndex = indexPath.row;
I believe indexPath.row should return the index of the name that was tapped in the Table View Controller.
#import "ContactsViewController.h"
#import "Contacts-Swift.h"
#interface ContactsViewController ()
#property (nonatomic, readwrite, strong) NSMutableArray* contacts;
#property (nonatomic, readwrite) NSInteger myIndex;
#end
#implementation ContactsViewController
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
/*NSArray *contactArray = #[#"Johnny Appleseed", #"Paul Bunyan", #"Calamity Jane"];
_contacts = [NSMutableArray arrayWithArray:contactArray];*/
/*Contact *c1 = [[Contact alloc] initWithName: #"Johnny"];
Contact *c2 = [[Contact alloc] initWithName: #"Paul Bunyan"];
Contact *c3 = [[Contact alloc] initWithName: #"Calamity Jane"];*/
// _contacts = [NSMutableArray arrayWithArray: #[c1, c2, c3]];
self.contacts = [NSMutableArray array];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.tableView registerClass: [UITableViewCell class]
forCellReuseIdentifier:#"UITableViewCell"];
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return self.contacts.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell" forIndexPath:indexPath];
Contact *contact = self.contacts[indexPath.row];
cell.textLabel.text = contact.name;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ContactsViewController *viewController = [self.navigationController.storyboard instantiateViewControllerWithIdentifier:#"the"];
[self.navigationController pushViewController:viewController animated:YES];
_myIndex = indexPath.row;
}
File: ContactsViewController.h
This is the header file that I am using in order to have access to the objective C methods and variables when working in the swift file. (I am not very familiar with objective C so there is a strong possibility that this implementation is what is causing my problems).
#import <UIKit/UIKit.h>
#interface ContactsViewController : UITableViewController <UITableViewDelegate>
#property (nonatomic, readonly) NSInteger myIndex;
#property (nonatomic, readonly, strong) NSMutableArray* contacts;
#end
File: ExistingContactViewController.swift
In the ExistingContactViewController I am just trying to set the firstName label equal to the text that is present in the contacts array at indexPath.row (in the ContactsViewController.m file).
import UIKit
#objc class ExistingContactViewController: UIViewController {
var contactsObject = ContactsViewController()
#IBOutlet weak var firstName: UILabel!
#IBOutlet weak var lastName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
print("\(contactsObject.myIndex)")
firstName.text = contactsObject.contacts[contactsObject.myIndex] as? String
}
When clicking names that are added to the Table View Controller the only index that is ever printed
print("\(contactsObject.myIndex)")
is 0. Which tells me that I am not capturing the index of the name that is tapped.
Image of myStoryboard The bottom most scene is the one that I am trying to change the First Name label to display the name of the cell that was tapped.
I have not yet been able change the title of this label when clicking on a cell. I have been able to implement this functionality when using just swift files (through watching numerous videos). I am sure that there is a key concept I am missing in the objective C files so any suggestions and/or pointers are much appreciated. If any additional details are needed let me know!
Thanks.
Seems that in your didSelectRowAtIndexPath: you are pushing a ContactsViewController to the navigation stack and if I understand correctly it should be instance of ExistingContactViewController. Also you should set the contactsObject property of the ExistingContactViewController before pushing it in the navigation stack (before viewDidLoad is executed) otherwise it's value will always be a new ContactsViewController which, probably, is causing the issue.
I hope this helps!

Mac OSX Storyboard : communicate between NSViewController

I use storyboard in a OS X cocoa application project with a SplitView controller and 2 others view controller LeftViewController and RightViewController.
In the LeftViewController i have a tableView that display an array of name. The datasource and delegate of the tableview is the LeftViewController.
In the RightViewController i just have a centered label that display the select name. I want to display in the right view the name selected in the left view.
To configure the communication between the 2 views controllers i use the AppDelegate and i define 2 property for each controller in AppDelegate.h
The 2 property are initialized in the viewDidLoad of view controller using the NSInvocation bellow :
#implementation RightViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
id delg = [[NSApplication sharedApplication] delegate];
SEL sel1 = NSSelectorFromString(#"setRightViewController:");
NSMethodSignature * mySignature1 = [delg methodSignatureForSelector:sel1];
NSInvocation * myInvocation1 = [NSInvocation
invocationWithMethodSignature:mySignature1];
id me = self;
[myInvocation1 setTarget:delg];
[myInvocation1 setSelector:sel1];
[myInvocation1 setArgument:&me atIndex:2];
[myInvocation1 invoke];
}
I have the same in LeftViewController.
Then if i click on a name in the table view, i send a message to the delegate with the name in parameter and the delegate update the label of the RightViewController with the given name. It works fine but according to apple best practice it’s not good.
Is there another way to communicate between 2 view controller inside a storyboard ?
I've already read a lot of post but found nothing for OS X.
You can download the simple project here : http://we.tl/4rAl9HHIf1
This is more advanced topic of app architecture (how to pass data).
Dirty quick solution: post NSNotification together with forgotten representedObject:
All NSViewControllers have a nice property of type id called representedObject. This is one of the ways how to pass data onto NSViewController. Bind your label to this property. For this simple example we will set representedObject some NSString instance. You can use complex object structure as well. Someone can explain in comments why storyboards stopped to show representedObject (Type safety in swift?)
Next we add notification observer and set represented object in handler.
#implementation RightViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserverForName:#"SelectionDidChange" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
//[note object] contains our NSString instance
[self setRepresentedObject:[note object]];
}];
}
#end
Left view controller and its table:
Once selection changes we post a notification with our string.
#interface RightViewController () <NSTableViewDelegate, NSTableViewDataSource>
#end
#implementation RightViewController
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return [[self names] count];
}
- (nullable id)tableView:(NSTableView *)tableView objectValueForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
{
return [self names][row];
}
- (NSArray<NSString *>*)names
{
return #[#"Cony", #"Brown", #"James", #"Mark", #"Kris"];
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
NSTableView *tableView = [notification object];
NSInteger selectedRow = [tableView selectedRow];
if (selectedRow >= 0) {
NSString *name = [self names][selectedRow];
if (name) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"SelectionDidChange" object:name];
}
}
}
PS: don't forget to hook tableview datasource and delegate in storyboard
Why is this solution dirty? Because once your app grows you will end up in notification hell. Also view controller as data owner? I prefer window controller/appdelegate to be Model owner.
Result:
AppDelegate as Model owner.
Our left view controller will get it's data from AppDelegate. It is important that AppDelegate controls the data flow and sets the data (not the view controller asking AppDelegate it's table content cause you will end up in data synchronization mess). We can do this again using representedObject. Once it's set we reload our table (there are more advanced solutions like NSArrayController and bindings). Don't forget to hook tableView in storyboard. We also modify tableview's delegate methos the tableViewSelectionDidChange to modify our model object (AppDelegate.selectedName)
#import "LeftViewController.h"
#import "AppDelegate.h"
#interface LeftViewController () <NSTableViewDelegate, NSTableViewDataSource>
#property (weak) IBOutlet NSTableView *tableView;
#end
#implementation LeftViewController
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return [[self representedObject] count];
}
- (nullable id)tableView:(NSTableView *)tableView objectValueForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
{
return [self representedObject][row];
}
- (void)setRepresentedObject:(id)representedObject
{
[super setRepresentedObject:representedObject];
//we need to reload table contents once
[[self tableView] reloadData];
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
NSTableView *tableView = [notification object];
NSInteger selectedRow = [tableView selectedRow];
if (selectedRow >= 0) {
NSString *name = [self representedObject][selectedRow];
[(AppDelegate *)[NSApp delegate] setSelectedName:name];
} else {
[(AppDelegate *)[NSApp delegate] setSelectedName:nil];
}
}
In RightViewController we delete all code. Why? Cause we will use binding AppDelegate.selectedName <--> RightViewController.representedObject
#implementation RightViewController
#end
Finally AppDelegate. It needs to expose some properties. What is interesting is how do I get my hands on all my controllers? One way (best) is to instantiate our own window controller and remember it as property. The other way is to ask NSApp for it's windows (be careful here with multiwindow app). From there we just ask contentViewController and loop through childViewControllers. Once we have our controllers we just set/bind represented objects.
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (nonatomic) NSString *selectedName;
#property (nonatomic) NSMutableArray <NSString *>*names;
#end
#import "AppDelegate.h"
#import "RightViewController.h"
#import "LeftViewController.h"
#interface AppDelegate () {
}
#property (weak, nonatomic) RightViewController *rightSplitViewController;
#property (weak, nonatomic) LeftViewController *leftSplitViewController;
#property (strong, nonatomic) NSWindowController *windowController;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
_names = [#[#"Cony", #"Brown", #"James", #"Mark", #"Kris"] mutableCopy];
_selectedName = nil;
NSStoryboard *storyboard = [NSStoryboard storyboardWithName:#"Main"
bundle:[NSBundle mainBundle]];
NSWindowController *windowController = [storyboard instantiateControllerWithIdentifier:#"windowWC"];
[self setWindowController:windowController];
[[self windowController] showWindow:nil];
[[self leftSplitViewController] setRepresentedObject:[self names]];
[[self rightSplitViewController] bind:#"representedObject" toObject:self withKeyPath:#"selectedName" options:nil];
}
- (RightViewController *)rightSplitViewController
{
if (!_rightSplitViewController) {
NSArray<NSViewController *>*vcs = [[[self window] contentViewController] childViewControllers];
for (NSViewController *vc in vcs) {
if ([vc isKindOfClass:[RightViewController class]]) {
_rightSplitViewController = (RightViewController *)vc;
break;
}
}
}
return _rightSplitViewController;
}
- (LeftViewController *)leftSplitViewController
{
if (!_leftSplitViewController) {
NSArray<NSViewController *>*vcs = [[[self window] contentViewController] childViewControllers];
for (NSViewController *vc in vcs) {
if ([vc isKindOfClass:[LeftViewController class]]) {
_leftSplitViewController = (LeftViewController *)vc;
break;
}
}
}
return _leftSplitViewController;
}
- (NSWindow *)window
{
return [[self windowController] window];
}
//VALID SOLUTION IF YOU DON'T INSTANTIATE STORYBOARD
//- (NSWindow *)window
//{
// return [[NSApp windows] firstObject];
//}
#end
Result: works exactly the same
PS: If you instantiate own window Controller don't forget to delete initial controller from Storyboard
Why is this better? Cause all changes goes to model and models sends triggers to redraw views. Also you will end up in smaller view controllers.
What can be done more? NSObjectController is the best glue between your model objects and views. It also prevents retain cycle that sometimes can happen with bindings (more advanced topic). NSArrayController and so on...
Caveats: not a solution for XIBs
I managed to get what i want by adding the following code in AppDelegate.m :
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
//
NSStoryboard *storyboard = [NSStoryboard storyboardWithName:#"Main"
bundle:[NSBundle mainBundle]];
self.windowController = [storyboard instantiateControllerWithIdentifier:#"windowController"];
self.window = self.windowController.window;
self.splitViewController = (NSSplitViewController*)self.windowController.contentViewController;
NSSplitViewItem *item0 = [self.splitViewController.splitViewItems objectAtIndex:0];
NSSplitViewItem *item1 = [self.splitViewController.splitViewItems objectAtIndex:1];
self.leftViewController = (OMNLeftViewController*)item0.viewController;
self.rightViewController = (OMNRightViewController*)item1.viewController;
[self.window makeKeyAndOrderFront:self];
[self.windowController showWindow:nil];
}
We also need to edit the storyboard NSWindowController object as follow :
Uncheck the checkbox 'Is initial controller' because we add it programmatically in AppDelegate.m.
Now the left and right view can communicate. Just define a property named rightView in OMNLeftViewController.h :
self.leftViewController.rightView = self.rightViewController;

How to get a dynamically changing NSTableView in OSX

I am new to coding for Mac. I am familiar with UITableView's from iOS coding, but can't seem to get NSTableView to work properly on my app. Yes I have looked through documentation and tutorials, but I am having trouble getting the result I need. My desired outcome should be pretty basic and simple, but I am stumped on the TableViews. I would really like it to function more like how it does with a UITableView rather than how it seems to want to function with NSTableView. I don't want to add or remove rows. I only need 1 column and I would really like for it to work like a list of buttons that can be pressed and trigger the content associated with that option on the same screen on the next NSView over.
(I have 3 views on one screen. The first is the main menu to the left and when pressed triggers the tableView to display the submenu. When that option is selected it opens the content on the far right of the screen. My desired result anyways.)
Currently nothing is populating the tableview. This is what it currently looks like.
.h
#interface RootViewController : NSViewController <NSTableViewDataSource>{
IBOutlet NSView *mainMenuView;
IBOutlet NSTableView *tableView;
NSMutableArray *options;
}
-(IBAction)mainMenuBtn:(id)sender;
#end
.m
#interface RootViewController ()
#end
#implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
options = [[NSMutableArray alloc] init];
}
return self;
}
-(void)viewDidLoad{
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
return [options count];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
return [[options objectAtIndex:row] valueForKey:[tableColumn identifier]];
}
-(IBAction)mainMenuBtn:(id)sender{
NSLog(#"1");
[options addObject:#"Strain 1"];
NSString *test = [options objectAtIndex:0];
NSLog(test);
[tableView reloadData];
}
Currently I have tableView connected in the NIB to the TableView. I attempted to connect dataSource to File's Owner, but that didn't work. Can't seem to find any place else to hook it up too. Any suggestions would be greatly appreciated. Thanks
First the dataSource of the NSTableView must be connected to your RootViewController instance in Interface Builder, or through code.
Something that seems a little suspicious to me is the line:
return [[options objectAtIndex:row] valueForKey:[tableColumn identifier]];
I don't know what is the value for the [tableColumn identifier] but it must match the property of the objects you're adding to your array. I can see that the objects you're adding are strings, so [tableColumn identifier] must be a property of class NSString. If you're using a string as the array's object, try using this:
return [options objectAtIndex:row];

Taking contact information and putting it into a UITableView

I am creating an app where you press a button and it opens up your contacts list. You can then select the contact you want to add and it imports their name and email into the app. I currently have that information going into labels but I want to add it to a table view cell. How would I do this?
My Code:
.h:
#import <UIKit/UIKit.h>
#import <AddressBookUI/AddressBookUI.h>
#interface FirstViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate>
- (IBAction)showPicker:(id)sender;
#property (weak, nonatomic) IBOutlet UILabel *firstName;
#property (weak, nonatomic) IBOutlet UILabel *email;
#end
.m:
#import "FirstViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize firstName;
#synthesize email;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)showPicker:(id)sender {
ABPeoplePickerNavigationController *picker =
[[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentModalViewController:picker animated:YES];
}
- (void)peoplePickerNavigationControllerDidCancel:
(ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
[self displayPerson:person];
[self dismissModalViewControllerAnimated:YES];
return NO;
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
return NO;
}
- (void)displayPerson:(ABRecordRef)person
{
NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,
kABPersonFirstNameProperty);
self.firstName.text = name;
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
NSString *emailId = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, 0);//0 for "Home Email" and 1 for "Work Email".
self.email.text = emailId;
}
#end
OK, I am going to explain how you programmatically implement a very basic table view controller. It will be up to you, though, to figure out how to integrate this into your application.
Let's start with the header file, let's call it MyTableViewController.h:
#interface MyTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
}
#end
As you can see, your controller class adopts the protocols UITableViewDelegate and UITableViewDataSource.
Now let's look at a first snippet from the implementation file MyTableViewController.m. Your first job, obviously, is to create the controller's view. You do this in your controller's loadView method. If you want to learn more about the view life cycle and how to program a UIViewController I suggest you read the UIViewController class reference and the accompanying View Controller Programming Guide.
- (void) loadView
{
// Give the view some more or less arbitrary initial size. It will be
// resized later when it is actually displayed
CGRect tableViewFrame = CGRectMake(0, 0, 320, 200);
UITableView* tableView = [[[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStyleGrouped] autorelease];
self.view = tableView;
// Here we make sure that the table view will take as much horizontal
// and vertical space as it can get when it is resized.
UIViewAutoresizing autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
tableView.autoresizingMask = autoresizingMask;
// We need to tell the table view that we are both its delegate and
// its data source.
tableView.delegate = self;
tableView.dataSource = self;
}
Just to let you know: You can omit loadView entirely if your controller is a subclass of UITableViewController, but I deliberately do not take that shortcut so that I can show you how a table view needs a delegate and a data source. Most important ist the data source.
In the next snippet in MyTableViewController.m we are going to implement some basic UITableViewDataSource methods. For this you need to understand how a table view is structured: A table view is divided into sections, and each section has a number of cells. The point of having sections is to visually separate groups of cells, with an optional section header or footer. I am not going into details here, though, to keep this simple.
- (NSInteger) numberOfSectionsInTableView:(UITableView*)tableView
{
// Let's keep it simple: We want just one section
return 1;
}
- (NSInteger) tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
// Let's keep it simple: We want just one row, or table view cell.
// Since we only have one section (see above) we don't have to look
// at the section parameter.
return 1;
}
And now, finally, the centerpiece where you create your table view cell. Again, this is a UITableViewDataSource method that we implement. Note that we do not need to inspect the indexPath parameter only because we know that we only have one section and one row. In a real world application you will probably have to write switch-case or if-else statements that examine indexPath.section and indexPath.row so that you can distinguish between the different cells you need to create.
- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
// This is very important for your future table view implementations:
// Always ask the table view first if it already has a cell in its
// cache. If you don't do this your table view will become slow when
// it has many cells.
NSString* identifier = #"MyTableViewCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
// Aha, the table view didn't have a cell in its cache, so we must
// create a new one. We use UITableViewCellStyleValue1 so that the
// cell can display two pieces of information.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];
}
// Regardless of whether we got the cell from the table view's cache
// or create a new cell, we must now fill it with content.
// First, obtain the information about the person from somewhere...
NSString* personName = ...;
NSString* personEmail = ...;
// ... then add the information to the table cell
cell.textLabel.text = personName;
cell.detailTextLabel.text = personEmail;
return cell;
}
As a final nicety, we implement a UITableViewDelegate method:
- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
// Here you can react to the user tapping on the cell. If you
// don't want the user to be able to select a cell you can
// add the following line to tableView:cellForRowAtIndexPath:
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
It is difficult to tell how you should integrate this into your application. It all depends where you want to display the table view. Since you say you want to replace the two labels you already have, one possible approach could be this:
In Interface Builder, add the table view as a subview to the main view of your FirstViewController
Add an outlet to FirstViewController that you connect to the table view
Let FirstViewController adopt the protocols UITableViewDelegate and UITableViewDataSource
Connect FirstViewController to the delegate and data source outlets of the table view
Don't implement loadView from my example, you don't need it, you already have made all the connections etc. in Interface Builder
If you need further help with integration, I suggest that you ask a new question and possibly refer to this answer. Good luck.

Can't reload Table View in tab bar controller

Hi I have a tab tab controller and my first tab includes a view with:
3 text fields
a submit button
a tableView
Once I fill in the text fields I click submit and it adds the information to my managedObjectContext which is an sqlite database (CoreData).
As soon as I click submit I want the tableView to reload to include the added object. Currently my tableView will display the data in the database but it will only add the new row when I stop and re-run the simulator
This is the code for when the add button is tapped, it is here that I can't get the reload tableView working because it says tableView is an undeclared identifier, what have i missed?
-(IBAction)addButtonTapped:(id)sender {
NSLog (#"Add Button Tapped");
NSLog(#"Adding %# units of item code %# at $%# each",quantityTextField.text,productTextField.text,priceTextField.text);
Products_MarketAppDelegate* delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext* managedObjectContext = delegate.managedObjectContext;
NSManagedObject* newProduct;
newProduct = [NSEntityDescription insertNewObjectForEntityForName:#"Product" inManagedObjectContext:managedObjectContext];
[newProduct setValue:productTextField.text forKey:#"itemCode"];
[newProduct setValue:quantityTextField.text forKey:#"quantity"];
[newProduct setValue:priceTextField.text forKey:#"price"];
if ([managedObjectContext hasChanges])
NSLog(#"Managed Object Changed");
NSError* error;
[managedObjectContext save:&error];
// Insert Reload Table Code Here
// ** I have tried the following and it gives an error "Use of undeclared identifier 'tableView'"
//[tableView reloadData];
//[self.tableView reloadData];
}
As you can see below I have added the UITableViewDelegate & UITableViewDataSource in the header file. I have also hooked up the tableview in IB so that the delegate and datasource connections are linked to file's owner.
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#interface FirstViewController : UIViewController
<UIApplicationDelegate, UITableViewDataSource,UITableViewDelegate,NSFetchedResultsControllerDelegate>
{
IBOutlet UITextField *productTextField;
IBOutlet UITextField *quantityTextField;
IBOutlet UITextField *priceTextField;
NSMutableArray *items;
NSFetchedResultsController *fetchedResultsController;
}
#property (nonatomic, retain) NSMutableArray *items;
#property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
-(IBAction)addButtonTapped:(id)sender;
#end
This is the code to fill the tableView which works correctly
#pragma mark TableView
-(NSInteger)numberOfSectionsInTableView: (UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell
Product* productItem =[fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:#"%# x %# # $%#",productItem.quantity,productItem.itemCode,productItem.price];
return cell;
}
I have searched for answers on this site and on others but I must be doing something different and the solutions aren't helping me
Your UIViewController does not currently have an instance variable pointing to your tableview. Set one up:
#property (nonatomic, retain) IBOutlet UITableView *myTableView;
Remember to synthesize this in your .m
#synthesize myTableView;
Then in your code you can call
[self.myTableView reloadData];
You might have got confused by looking at code examples that use a UITableViewController instead of a UIViewController. The UITableViewController already has an instance variable called tableView, so your subclass wouldn't need it's own tableView instance variable declared. But you're using a UIViewController, so you must declare a tableView instance variable.
Thanks #MattyG for all your help. At first I wasn't sure if I was going against the norm and thats why it wasn't working.
I ended up solving the problem due to your suggestions & it works perfectly! I used the debugger and found that that although we had created a property for the table I had not created an IBOutlet and linked it in my nib file with:
IBOutlet UITableView *myTableView;
I guess this meant that I was telling myTableView to reload but it wasn't hooked up to my table and thus couldn't use the datasource methods.