First off good day and thanks for taking the time to answer my question.
Some background: I have done the same thing in a regular OS X program in that I run everything in main.m
What I'm doing:
making a VERY crude app that adds a FirstName and LastName text field. The user enters some text and when they press add to address book the program gets the string info and adds it to a NSMutableArray
like i said VERY crude but for learning purposes.
What's wrong:
AddressBook.h and AddressBook.m files
AddressBook.h
#import <Foundation/Foundation.h>
@interface AddressBook : UIViewController
{
NSMutableArray *nameArray;
}
@property (nonatomic, copy) NSMutableArray *nameArray;
@end
AddressBook.m
#import "AddressBook.h"
@implementation AddressBook
@synthesize nameArray;
@end
side notes and comments
So I get that I create a class called AddressBook and it has a NSMutableArray
I know that if I wanted to create a new object that is part of AddressBook class I would call it in main.m like this AddressBook *newAddressBook = [AddressBook new]; newAddressBook.nameArray = [NSMutableArray array]; then i could do what I want. I have successfully made a program like this but not an iPhone version.
BIDViewController.h
#import <UIKit/UIKit.h>
#import "AddressBook.h"
@interface BIDViewController : UIViewController
{
AddressBook *myBook;
UITextField *firstNameField;
UITextField *lastNameField;
NSString *tester;
id myTempBook;
}
//@property (retain, nonatomic) AddressBook *myBook;
@property (retain, nonatomic) IBOutlet UITextField *firstNameField;
@property (retain, nonatomic) IBOutlet UITextField *lastNameField;
@property (copy, nonatomic) NSString *tester;
@property (retain, nonatomic) id myTempBook;
- (IBAction)addToAddressButton:(id)sender;
- (IBAction)textFieldDoneEditing:(id)sender;
- (IBAction)displayAddress:(id)sender;
@end
BIDViewController.m
#import "BIDViewController.h"
@implementation BIDViewController
@synthesize lastNameField, firstNameField, tester;
//@synthesize myBook;
@synthesize myTempBook;
- (IBAction)textFieldDoneEditing:(id)sender
{
//keyboard stuff
//give control back to view
[sender resignFirstResponder];
}
- (IBAction)displayAddress:(id)sender
{
//display address book array
}
- (IBAction)addToAddressButton:(id)sender
{
//add the names to array
//here is what I want to do
[myBook.nameArray addObject:lastNameField.text];
//ever time I NSLog the array with objectAtIndex:0
//it returns null. I've tried EVERY combinations of stuff I even tried
//allocating a new array
//such as myBook.nameArray = [NSMutableArray array]; and I still could not return anything but null :(
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
//myBook.nameArray = [NSMutableArray array];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
My Question:
what am I doing wrong because I have accomplished this exact same concept but running the program in main.m.
I think I understand what is going on...
- iPhone program loads the BIDViewController when the user clicks on the app icon.
- since BIDViewController gets initialized it creates myBook which is an object in class AddressBook. it is automatically getter and setter with @synthesize
- my BIDViewController sees that it is created and I can write to the array with no problem however somewhere along the way I keep getting null returns.
Other side notes
I can share my program where I have successfully accomplished what I wanted in a non iPhone app but a simple command console version.