UITableView in a UIViewController on an iPad - objective-c

I'm trying to develop a tiny app to manage the registry for a teacher. My app works so:
I present a UIViewController (only landscape) to select the class.
When I press the button (for example: 1F), I call a method to parse an XML file in which I got the name and the surname of the students. So I put this name and surname into an NSArray of NSDictionary (arrayStudents[0] = dictStudents and dictStudents is composed by 3 key: number, name and surname).
In the second UIViewController I put a UITableView on the left of the screen and a normal view to the right. In the UITableView I want to have the name and surname of the students and on the right I want to show the score about test of the student selected by tapping on the UITableView.
This is how my viewController looks like:
My problem is how to populate the UITableView. I connected the tableView with an IBOutlet to my second viewController and then I connect the delegate and the dataSource to the second viewController, but the app is still crashing. I will post here the code of my table view class and second view controller, so you can help me to fix this problem.
Here you can see the connection tab of my tableView:
tableView.h
#import <UIKit/UIKit.h>
#interface tableView : UITableView <UITableViewDelegate,UITableViewDataSource>
#property (nonatomic, strong) NSMutableArray *arrayStudents;
#end
tableView.m
#import "tableView.h"
#implementation tableView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.arrayStudents.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self.arrayStudents[indexPath.row] objectForKey:#"name"];
cell.detailTextLabel.text = [self.arrayStudents[indexPath.row] objectForKey:#"surname"];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
#end
detailsViewController.h
#import <UIKit/UIKit.h>
#interface DetailsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *tableViewStudentsNameAndSurname;
#property (weak, nonatomic) IBOutlet UIView *viewDetails;
#property (weak, nonatomic) IBOutlet UILabel *labelName;
#property (weak, nonatomic) IBOutlet UILabel *labelSurname;
#property (weak, nonatomic) IBOutlet UITextField *textFieldTest1;
#property (weak, nonatomic) IBOutlet UITextField *textFieldTest2;
#property (weak, nonatomic) IBOutlet UITextField *textFieldTest3;
#property (weak, nonatomic) IBOutlet UITextField *textFieldText4;
#property (weak, nonatomic) IBOutlet UITextView *textViewValutation;
#property (weak, nonatomic) IBOutlet UILabel *labelTotalScore;
#property (nonatomic, strong) NSString *fileName;
- (IBAction)saveDataToPlistFile:(id)sender;
#end
detailsViewController.m
#import "DetailsViewController.h"
#import "ParserXML.h"
#import "tableView.h"
#interface DetailsViewController () {
ParserXML *parser;
tableView *table;
}
#end
#implementation DetailsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
parser = [[ParserXML alloc] init];
NSLog(#"DetailsViewController: %#", self.fileName);
parser.fileName = self.fileName;
[parser parseXML];
table = [[tableView alloc] init];
table.arrayStudents = [parser.arrayStudents mutableCopy];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)saveDataToPlistFile:(id)sender {
}
#end
ParserXML.h
#import <Foundation/Foundation.h>
#interface ParserXML : NSObject <NSXMLParserDelegate>
#property (nonatomic, strong) NSMutableArray *arrayStudents;
#property (nonatomic, strong) NSDictionary *dictStudents;
#property (nonatomic, strong) NSString *fileName;
- (void) parseXML;
#end
ParserXML.m
#import "ParserXML.h"
#interface ParserXML() {
NSXMLParser *nameStudentParser;
NSString *pathXmlFile;
}
#end
#implementation ParserXML
- (id) init {
self = [super init];
if (self) {
self.arrayStudents = [[NSMutableArray alloc] init];
pathXmlFile = [[NSBundle mainBundle] pathForResource:self.fileName ofType:#"xml"];
}
return self;
}
- (void) parseXML {
NSURL *xmlUrl = [NSURL URLWithString:pathXmlFile];
NSString *host = [xmlUrl host];
if (xmlUrl == nil || host == nil) {
NSData *data = [[NSData alloc] initWithContentsOfFile:pathXmlFile];
nameStudentParser = [[NSXMLParser alloc] initWithData:data];
} else {
nameStudentParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlUrl];
}
[nameStudentParser setDelegate:self];
[nameStudentParser setShouldProcessNamespaces:NO];
[nameStudentParser setShouldReportNamespacePrefixes:NO];
[nameStudentParser setShouldResolveExternalEntities:NO];
[nameStudentParser parse];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser {
NSLog(#"File trovato inizio il parsing del documento");
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSLog(#"Errore Parsing");
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:#"students"]) {
} else if ([elementName isEqualToString:#"student"]) {
NSString *numberValue = attributeDict[#"number"];
NSString *nameValue = attributeDict[#"name"];
NSString *surnameValue = attributeDict[#"surname"];
//Inserire dizionario di array
self.dictStudents = #{#"number": numberValue,
#"name": nameValue,
#"surname" : surnameValue};
[self.arrayStudents addObject:self.dictStudents];
NSLog(#"arrayStudents dim = %d", self.arrayStudents.count);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(#"ArrayFuels = %#", self.arrayStudents);
}
#end
When I run the app Xcode says:
[DetailsViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x751da40
Can you help me to fix that problem? What I'm doing wrong?

Okay, So I'm pretty new to objective C so this is not an expert opinion. But from what I get, it is looking for the delegate method that should be there in DetailsViewController class. You have connected your table view delegate/datasource to DetailsViewController class, I think you're supposed to specify the class as being the delegate class of UITableView as well.
In DetailsViewController.h:
#interface DetailsViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
and then plug in the UITableView Delegate methods in DetailsViewController.m file.
Although, what I don't get is why are you using 2 table view classes? If you want to use tableView class, why not initialize in DetailsViewController::viewDidLoad and simply:
[self.view addSubView:tableView]
I don't think there is a need for tableViewStudentsNameAndSurname in your class.

Related

Objective-c: passing data from UITable to ViewController with prepareForSegue

this is my very first app and, basically, this part consists in passing data from a UItableView to a second View Controll. I managed to learn how to pass data from a simple NSarray (also in a UITable), but my goal is to pass values from a NSDictionary. Everything is set up, but I can't figure out how to write the PrepareForSegue method properly. The app runs, but the label on the "DetailView" stays empty. What I got so far:
#implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
_citySpots = #{#"Bars" : #[#"Hurricane", #"Black Swan", #"Texas"],
#"Clubs" : #[#"Electric Wizard", #"Offspring", #"The Tunnel"],
#"Restaurants" : #[#"Nando's", #"1/2 Burguer", #"Satellite"],
};
_sectionTitles = [[_citySpots allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
}
PrepareForSegue Method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"spotsDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *destViewController = segue.destinationViewController;
NSString *sectionTitle = [_sectionTitles objectAtIndex:indexPath.section];
NSArray *citySpots = [_citySpots objectForKey:sectionTitle];
destViewController.receiver = [citySpots objectAtIndex:indexPath.row];
}
}
And the receiver(header):
#import <UIKit/UIKit.h>
#import "TableViewController.h"
#interface DetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *receiver;
#property (nonatomic, strong) NSString *spot;
#end
Main:
#import "DetailViewController.h"
#interface DetailViewController ()
#end
#implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
_receiver.text =_spot;
}
Can someone help me out? Thanks
Try to use setters:
[destViewController setReceiver:[citySpots objectAtIndex:indexPath.row]];
[destViewController setSpot:[citySpots objectAtIndex:indexPath.row]];

table view no data showing in the table cell

I have face problem in that code when i have run that code then no data show in tableview on it's table cell. there is no error in console but data not show. please Guide me what's the mistake i have do that code.
InvoiceCell.h
#import <UIKit/UIKit.h>
#interface InvoiceCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *ItemImage;
#property (strong, nonatomic) IBOutlet UILabel *ItemName;
#property (strong, nonatomic) IBOutlet UILabel *ItemType;
#property (strong, nonatomic) IBOutlet UILabel *date;
#property (strong, nonatomic) IBOutlet UILabel *Amount;
#property (strong, nonatomic) IBOutlet UILabel *status;
#end
InvoiceCell.m
#import "InvoiceCell.h"
#implementation InvoiceCell
#synthesize ItemImage,ItemName,ItemType,Amount,status;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
InvoiceViewController.h
#import <UIKit/UIKit.h>
#interface InvoiceViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic,strong) NSArray *DataImages;
#property (nonatomic, strong) NSArray *DataName;
#property (nonatomic, strong) NSArray *Datatype;
#property (nonatomic, strong) NSArray *DataAmount;
#property (nonatomic, strong) NSArray *DataStatus;
#property (nonatomic, strong) NSArray *Datadate;
#end
InvoiceViewController.m
#import "InvoiceViewController.h"
#import "InvoiceCell.h"
#interface InvoiceViewController ()
#end
#implementation InvoiceViewController
#synthesize DataImages,DataName,Datatype,DataAmount,DataStatus,Datadate;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
DataImages = #[#"img1.jpg", #"img2.jpg", #"img3.jpg", #"img4.jpg", #"img5.jpg"];
DataName = #[#"Suit", #"Shirt", #"paint", #"Suit", #"Suit"];
Datatype = #[#"Brown", #"Black", #"Blue", #"Black", #"Blue"];
DataAmount = #[#"$500", #"$400",#"$500", #"$400",#"$300"];
DataStatus = #[#"Paid", #"Due", #"Paid", #"Due", #"Paid"];
Datadate = #[#"8/5/2014", #"10/6/2014",#"8/5/2014", #"10/6/2014",#"8/5/2014"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return DataName.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"InvoiceCell";
InvoiceCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
{
cell = [[InvoiceCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
long row = [indexPath row];
NSLog(#"%#", cell.ItemName.text = DataName[row]);
cell.ItemName.text = DataName[row];
cell.ItemType.text = Datatype[row];
cell.ItemImage.image = [UIImage imageNamed:DataImages[row]];
cell.date.text = Datadate[row];
cell.Amount.text = DataAmount[row];
cell.status.text = DataStatus[row];
return cell;
}
In the Debug Window and console Data show but not show data on simulator screen in the tableview cells
Thank you.
You may have a tableView in your StoryBoard, but you have no connection to it in your code. Add this to your InvoiceViewController.h
#property IBOutlet UITableView *tableView;
and hook it up to your TableView in Interface Builder.
Then you can set the DataSource and Delegate in your InvoiceViewController.m -[viewDidLoad] method
_tableView.delegate = self;
_tableView.dataSource = self;
Now the TableView will know to call the delegate methods you implemented and you should see your data in the table.

NSTableView Data Source Not Working

I am trying to populate an NSTableView with an NSArray. I have a view controller, which I have set as my datasource, but every time I run the program, I get this error: Must implement numberOfRowsInTableView: and tableView:objectValueForTableColumn:row:, both of which I have implemented.
Here is my header:
#import <Cocoa/Cocoa.h>
#interface ChatViewController : NSViewController <NSTableViewDataSource>
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView;
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex;
#end
And here is my implementation file:
#import "ChatViewController.h"
#import "Socket.h"
#interface ChatViewController ()
#property (strong) IBOutlet NSTableView *people;
#property (strong) IBOutlet NSTextField *message;
#property (strong) IBOutlet NSButton *send;
#property (strong) Socket *sock;
#property (strong, nonatomic) NSString *recievedText;
#property (strong, nonatomic) NSArray *tableData;
- (void)updateUI;
#end
#implementation ChatViewController
#synthesize sock;
#synthesize recievedText;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self.people setDataSource:self];
sock = [[Socket alloc] init];
[sock connectToServerWithIP:#"127.0.0.1" andPort:5001];
[self updateUI];
}
return self;
}
- (void)updateUI
{
[sock sendMessage:#"getPeople"];
recievedText = [NSString stringWithString:[sock recieveMessage]];
self.tableData = [recievedText componentsSeparatedByString:#";"];
NSLog(#"%#", self.tableData);
[self.people reloadData];
NSLog(#"%#", self.people);
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [self.tableData count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
return [self.tableData objectAtIndex:rowIndex];
}
#end
Socket is a class that I created for opening and closing sockets to a server, which is fully functional. The tableData array is populated like a normal array.
Thanks for any help.
at first make your table view delegate and data source of file's owner through xib.
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
if(aTableView == tableViewS)
{
if([[aTableColumn identifier] isEqualToString:#"first"])
{
return [self.tableData objectAtIndex:rowIndex];
}
}
return nil;
}
you need to give identifier name for table column
tableViewS is IBOutlet of table view.
First of you don't need to declare the methods in your .h file since they are part of the protocol your class is implementing. And also have you really set you data source? Have you done something like this some where?
self.tableView.dataSource = self;

UITableView isn't working

Im trying to make a comments UITableView inside a UIViewController but I'm having problems. If I set the text directly, it shows up (cell.username.text) but if I try to fill it with a PFQuery (parse.com) nothing shows up
//DetailViewController.h
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#interface DetailViewController : UIViewController{
}
#property (strong, nonatomic) PFObject *place;
#property (strong, nonatomic) IBOutlet PFImageView *userPhoto;
#property (strong, nonatomic) IBOutlet UILabel *username;
#property (strong, nonatomic) IBOutlet UILabel *message;
#property (strong, nonatomic) IBOutlet UILabel *distance;
#property (strong, nonatomic) IBOutlet UILabel *checkCount;
#property (strong, nonatomic) IBOutlet PFImageView *photo;
#property (strong, nonatomic) IBOutlet UIScrollView *scroller;
#property (strong, nonatomic) IBOutlet UITableView *commentsTableView;
- (IBAction)checkMarkButton:(UIButton *)sender;
#end
//DetailViewController.m
#import "DetailViewController.h"
#import "CommentsViewController.h"
#interface DetailViewController (){
CommentsViewController *test;
}
#end
#implementation DetailViewController
#synthesize place;
#synthesize userPhoto, message, username, checkCount, photo, scroller, commentsTableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
test = [[CommentsViewController alloc] init];
self.commentsTableView.delegate = test;
self.commentsTableView.dataSource = test;
[test commentsQuery:place];
}
//CommentsViewController.h
#import <Parse/Parse.h>
#interface CommentsViewController : UITableViewController{
}
#property (strong, nonatomic) PFObject *place;
- (void)commentsQuery:(PFObject *)object;
#end
//CommentsViewController.m
#import "CommentsViewController.h"
#import "DetailViewController.h"
#import "CommentsCell.h"
#interface CommentsViewController (){
NSArray *commentsArray;
}
#end
#implementation CommentsViewController
#synthesize place;
- (void)commentsQuery:(PFObject *)object {
place = object;
PFQuery *query1 = [PFQuery queryWithClassName:#"activity"];
[query1 whereKey:#"type" equalTo:#"comment"];
[query1 whereKey:#"place" equalTo:place];
[query1 orderByDescending:#"createdAt"];
[query1 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error){
commentsArray = [[NSArray alloc]initWithArray:objects];
NSLog(#"%lu", (unsigned long)[commentsArray count]);
}
}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [commentsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *CellIdentifier = #"Cell";
CommentsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[CommentsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
PFObject *tempObj = [commentsArray objectAtIndex:indexPath.row];
cell.username.text = #"username";
cell.comment.text = [tempObj objectForKey:#"content"];
cell.userThumbnail.file = [tempObj objectForKey:#"userThumbnail"];
[cell.userThumbnail loadInBackground];
return cell;
}
#end
My problem was calling reloadData. I followed the answer here and it works UITableView delegate and datasource in a separate class after parsing

Call method from another view, I think?

I have a simple utility app, with a MainViewController.m & h and a FlipsideViewController.m & h. Within my storyboard I have a button on MainViewController. I want to be able to click the button and run a method in FlipsideViewController.m is this possible? this is my first app and I am a total novice. all comments / suggestion welcome.
enter code here
i have this in my FlipsideViewController.m this is what i want to call when i click the button.
- (void)SaveFPQData
{
NSLog(#"Data Saved");
}
and this is what i have in MainViewController.m
- (IBAction)saveButton:(id)sender
{
}
This is the code I have so far;
MainViewController.h
#import "FlipsideViewController.h"
#import "sqlite3.h"
#import "FPQCheck.h"
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate>
#property (weak, nonatomic) IBOutlet UITextField *nameField;
#property (weak, nonatomic) IBOutlet UITextField *checkField;
#property (weak, nonatomic) IBOutlet UITextField *commentsField;
#property (weak, nonatomic) FlipsideViewController *flipsidecontroller;
- (IBAction)saveButton:(id)sender;
- (IBAction)showHistoryButton:(id)sender;
#end
MainViewController.m
#import "MainViewController.h"
#import "FlipsideViewController.h"
#import "sqlite3.h"
#interface MainViewController ()
#end
#implementation MainViewController
- (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.
}
#pragma mark - Flipside View
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showAlternate"]) {
[[segue destinationViewController] setDelegate:self];
}
}
- (IBAction)saveButton:(id)sender
{
[self.flipsidecontroller SaveFPQData];
//[[NSNotificationCenter defaultCenter] postNotificationName:#"SaveFPQData" object:nil];
}
- (IBAction)showHistoryButton:(id)sender
{
}
#end
FlipSideViewController.h
#import <UIKit/UIKit.h>
#import "FPQCheck.h"
#class FlipsideViewController;
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#end
#interface FlipsideViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *myTableView;
#property (weak, nonatomic) id <FlipsideViewControllerDelegate> delegate;
-(void)SaveFPQData;
- (IBAction)done:(id)sender;
- (IBAction)deleteEntry:(id)sender;
#end
FlipSideViewController.m
#import "FlipsideViewController.h"
#import "MainViewController.h"
#interface FlipsideViewController ()
{
NSMutableArray *arrayOfCheck;
sqlite3 *fpqDB;
NSString *dbPathString;
}
#end
#implementation FlipsideViewController
- (void)viewDidLoad
{
/*
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(SaveFPQData)
name:#"SaveFPQData"
object:nil];
*/
[super viewDidLoad];
arrayOfCheck = [[NSMutableArray alloc]init];
[self creatOrOpenDB];
[[self myTableView]setDelegate:self];
[[self myTableView]setDataSource:self];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)SaveFPQData
{
NSLog(#"Data Saved");
}
-(void)creatOrOpenDB
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
dbPathString = [docPath stringByAppendingPathComponent:#"FPQ.db"];
char *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dbPathString]) {
const char *dbPath = [dbPathString UTF8String];
//create db
if (sqlite3_open(dbPath, &fpqDB)==SQLITE_OK) {
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS FPQ (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, CHECK INTEGER, COMMENTS TEXT)";
sqlite3_exec(fpqDB, sql_stmt, NULL, NULL, &error);
sqlite3_close(fpqDB);
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Actions
- (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];}
- (IBAction)deleteEntry:(id)sender {
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayOfCheck count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
FPQCheck *fpqCheck = [arrayOfCheck objectAtIndex:indexPath.row];
NSString *nameANDcheck = [NSString stringWithFormat:#"%#%d", fpqCheck.name, fpqCheck.check];
cell.textLabel.text = nameANDcheck;
cell.detailTextLabel.text = fpqCheck.comments;
return cell;
}
#end
You have mainly two ways:
add a property (eg. self.flipSideController) to your MainViewController to store a reference to the FlipsideViewController; then call SaveFPQData though it (eg. [self.flipSideController SaveFPQData]; or
use notification center to post a notification from saveButton: that triggers SaveFPQData; this would go like this:
//-- in flipsidecontroller `viewDidLoad`:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(SaveFPQData)
name:#"SaveFPQData"
object:nil];
//-- in saveButton:
[[NSNotificationCenter defaultCenter] postNotificationName:#"SaveFPQData" object:nil];
The second method is the simplest to implement, IMO, and it allows for the loosest coupling, at the expense of some clock cycles.
EDIT:
It is not entirely clear to me what you are trying to do (specifically, I don't understand fully how you can push the button in MainViewController once you FlipsideViewController is displayed; on the other hand, if you do not segue to the FlipsideViewController, then it is not there, so you cannot send a message to it), anyway you could try and initialise your self.flipsideViewController property in prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showAlternate"]) {
UIViewController* controller = [segue destinationViewController];
[controller setDelegate:self];
if ([controller isKindOfClass:[FlipsideViewController class]])
self.flipsideViewController = (id)controller;
}
}
after doing that, your MainViewController will be able to send the saveFPQ message to the FlipsideViewController.
If you mean you would like to send the saveFPQ message before segue-ing to the FlipsideViewController, you should make the saveButton segue to it and the call the saveFPQ method.
What I suspect is you need some kind of "model" object accessible both from the main view and the flipside view controller.
Hope this helps.