saving data in textfields and creating delegates - iphone

Trying to get a textfield to save data automatically and store it, I want to be able to go back to the view and the data remain until its edited again.
so far I have this...
#interface TableDatabaseViewController : UIViewController <UITextFieldDelegate>
{
Tables*table;
NSArray * tables;
IBOutlet UITextField * surname;
IBOutlet UITextField * seatNo;
IBOutlet UISegmentedControl * occupied;
IBOutlet UITextField * numberLabel;
int tapCount;
id <TableInfoDelgate> modTable;
}
#property (nonatomic, retain) Tables*table;
#property (nonatomic, retain) NSArray * tables;
#property (nonatomic, retain) IBOutlet UITextField * surname;
#property (nonatomic, retain) IBOutlet UITextField * seatNo;
#property (nonatomic, retain) IBOutlet UISegmentedControl* occupied;
#property (nonatomic, retain) id <TableInfoDelgate> modTable;
#property (nonatomic, retain) IBOutlet UITextField * numberLabel;
#property int tapCount;
-(id)initWithTables:(Tables*)t;
//-(void)myFunction;
#end
#synthesize table, numberLabel,tables,tapCount, modTable, seatNo,surname, occupied;
- (void)dealloc
{
self.numberLabel =nil;
self.surname = nil;
self.table = nil;
self.seatNo = nil;
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-(void)myFunction
{
AddTableViewController * createController = [[AddTableViewController alloc] initWithNibName:#"AddTableViewController" bundle:nil];
[self.navigationController pushViewController:createController animated:YES];
[createController release];
}
- (void)viewDidLoad
{
UIBarButtonItem * theButton = [[UIBarButtonItem alloc] initWithTitle:#"Order" style:UIBarButtonItemStyleBordered target:self action:#selector(myFunction)];
self.navigationItem.rightBarButtonItem = theButton;
[theButton release];
self.tables = [TableListBuilder getTables];
self.numberLabel.text = [NSString stringWithFormat:#"%#", self.table.number];
self.seatNo.text = self.table.seats;
self.surname.text = self.table.surname;
// self.occupied.text = self.table.occupied;
[super viewDidLoad];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:NO];
[self.modTable modifyTableatIndex: nil andsurname:self.surname.text andseatNo:self.seatNo.text andoccupied: self.occupied];
}
I also created a delegate which is:
#protocol TableInfoDelgate <NSObject>
-(void) modifyTableatIndex: (int) index andsurname: (NSString*) surname andseatNo: (NSString*) seatNo andoccupied: (BOOL) occupied;
#end
data in the textfield is not saving. Thanks

What does this function do?
-(void) modifyTableatIndex: (int) index andsurname: (NSString*) surname andseatNo: (NSString*) seatNo andoccupied: (BOOL) occupied;
For storing simple values look into NSUserDefaults
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[textField setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"storedTextValue"];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:#"storedTextValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

Related

[__NSCFNumber length]: unrecognized selector sent to instance

I am trying to pass data from a table view controller to a detail view controller. Every entry of my table works just as it should, except for one.
Here is the prepareForSegue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"showDetails"]) {
NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
NSDictionary *notification = [[self notifications] objectAtIndex:[indexPath row]];
NRDetailViewController *destViewController = [segue destinationViewController];
[destViewController setDate:[notification objectForKey:#"date"]];
[destViewController setFrom:[notification objectForKey:#"from"]];
[destViewController setIden:[notification objectForKey:#"identifier"]];
[destViewController setPriority:[notification objectForKey:#"priority"]];
[destViewController setSubject:[notification objectForKey:#"subject"]];
[destViewController setMessage:[notification objectForKey:#"text"]];
}
}
Here is my interface for the detail view:
#import <UIKit/UIKit.h>
#interface NRDetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *dateField;
#property (weak, nonatomic) IBOutlet UITextField *fromField;
#property (weak, nonatomic) IBOutlet UITextField *idenField;
#property (weak, nonatomic) IBOutlet UITextField *prioField;
#property (weak, nonatomic) IBOutlet UITextField *subjectField;
#property (weak, nonatomic) IBOutlet UITextView *messageView;
#property (copy, nonatomic) NSString *date;
#property (copy, nonatomic) NSString *from;
#property (copy, nonatomic) NSString *iden;
#property (copy, nonatomic) NSString *priority;
#property (copy, nonatomic) NSString *subject;
#property (copy, nonatomic) NSString *message;
#end
Here is the detail view's implementation file:
#import "NRDetailViewController.h"
#interface NRDetailViewController ()
#end
#implementation NRDetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self dateField] setText:[self date]];
[[self fromField] setText:[self from]];
[[self idenField] setText:[self iden]];
[[self prioField] setText:[self priority]];
[[self subjectField] setText:[self subject]];
[[self messageView] setText:[self message]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I have problem setting the text of idenField to the text of iden. I have added an exception breakpoint, and got that the exception indeed occurs at this line:
[[self idenField] setText:[self iden]];
At this point, I have printed the value of [self iden] and it even contains the content I want to pass, so I have absolutely no idea what the problem is, since as I said, all the other fields are working as they should.
The exception being thrown is:
2013-08-07 22:28:20.539 NotificationReader[1214:11303] -[__NSCFNumber length]: unrecognized selector sent to instance 0x7587a00
Any help would be appreciated greatly.
It looks like:
[destViewController setIden:[notification objectForKey:#"identifier"]];
is returning an NSNumber and not an NSString. You could try replacing that line with:
[destViewController setIden:[(NSNumber*)[notification objectForKey:#"identifier"] stringValue]];
Somewhere you are passing an NSNumber instead of a NSString.
To set the text, iden needs to be a NSString. When you are getting the object from the NSDictionary, it is a NSNumber.
Try this:
[destViewController setIden:(NSString *)[notification objectForKey:#"identifier"]];
or change the iden property to be a NSNumber and then
[[self idenField] setText:[NSString stringWithFormat:#"%d", [self iden]]];

Passing object using story board iOS5

I am trying to pass an object to a static grouped table view that I have created in story board.
Here is the code that I am using in my first view to push the second view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Row Selected");
CustomerDetailTableViewController *detailView = [[self storyboard] instantiateViewControllerWithIdentifier:#"DetailsView"];
detailView.customer = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]];
NSLog(#"%#",detailView.customer.firstName);
[self.navigationController pushViewController:detailView animated:YES];
}
The NSlog for the firstName is correct but when the detail view is pushed the cells in the detailView are null. I'm probably just missing something dumb but a fresh set of eyes would be much appreciated.
Here is the code for the detailView controller:
CustomerDetailTableViewController.h
#class Customer;
#import <UIKit/UIKit.h>
#interface CustomerDetailTableViewController : UITableViewController{
Customer *customer;
UILabel *fullName;
UILabel *address;
UILabel *homePhone;
UILabel *cellPhone;
UILabel *email;
}
#property (nonatomic, strong) IBOutlet UILabel *fullName;
#property (nonatomic, strong) IBOutlet UILabel *address;
#property (nonatomic, strong) IBOutlet UILabel *homePhone;
#property (nonatomic, strong) IBOutlet UILabel *cellPhone;
#property (nonatomic, strong) IBOutlet UILabel *email;
#property (nonatomic, strong) Customer *customer;
#end
CustomerDetailTableViewController.m
#import "CustomerDetailTableViewController.h"
#import "Customer.h"
#implementation CustomerDetailTableViewController
#synthesize fullName, address, homePhone, cellPhone, email, customer;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
fullName = [NSString stringWithFormat:#"%# %#", customer.firstName, customer.lastName];
address = [NSString stringWithFormat: #"%#/n%#, %# %#", customer.address, customer.city, customer.state, customer.zipCode];
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (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 == UIInterfaceOrientationPortrait);
}
#end
So, you have created the two views (the first TableView and the CustomerDetailTableViewController) with storyboards?
In that case, you have to click on the connection line between the two views into the storyboard and set the "Identifier" field, into the section "Storyboard Segue", to something like "setCustomer". Here's a screenshot:
Here a little screenshot:
After that, you can comment the method tableView:didSelectRowAtIndexPath: on the first TableView, and replace with this method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"setCustomer"]) {
CustomerDetailTableViewController *customerDetailVC = (CustomerDetailTableViewController *)[segue destinationViewController];
customerDetailVC.customer = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]];
}
}
Remember to include
#include "CustomerDetailTableViewController.h"
at the top of implementation file.
I hope this help!

Passing data between two ViewControllers Problem

Hi I have two viewControllers.
NewsViewController
#import "SingleViewController.h"
#interface NewsViewController : UIViewController {
}
- (NSString *)getFirstImage:(NSString *)htmlString;
#end
and the implementation file:
#import "SingleViewController.h"
SingleViewController *singleViewController;
#interface NewsPadViewController () <UIActionSheetDelegate>
- (void)loadSubscriptions;
#property (nonatomic, assign) BOOL wrap;
#property (nonatomic, retain) NSMutableArray *items;
#end
#implementation NewsPadViewController
....CODE....
- (void)carouselCurrentItemTapped{
//[self addSubview:articolo];
MWFeedItem *item =[reader.feedItems objectAtIndex:[carousel currentItemIndex]];
NSLog(#"Current Item tappped, index: %d", [carousel currentItemIndex]);
singleViewController.prova.text=#"CIAO";
singleViewController.ciao=#"sample";
[singleViewController.web loadHTMLString:item.summary baseURL:nil];
NSLog(#"conternutio:");
NSLog(singleViewController.ciao);
}
SingleViewController.h
#import <UIKit/UIKit.h>
#interface SingleViewController : UIViewController{
#public
UIWebView *web;
UILabel *prova;
NSString *ciao;
}
#property (nonatomic,retain) IBOutlet UIWebView *web;
#property (nonatomic,retain) IBOutlet UILabel *prova;
#property (nonatomic,retain) IBOutlet NSString *ciao;
#end
and SingleViewController.m
#import "SingleViewController.h"
#implementation SingleViewController
#synthesize web,prova,ciao;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[prova setText:ciao];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Why I can't access from NewsPadViewController at object in SingleViewController? Where is my error? thanks
UPADATE
- (void)carouselCurrentItemTapped{
//[self addSubview:articolo];
MWFeedItem *item =[reader.feedItems objectAtIndex:[carousel currentItemIndex]];
NSLog(#"Current Item tappped, index: %d", [carousel currentItemIndex]);
SingleViewController *singleViewController = [[SingleViewController alloc] initWithNibName:#"SingleViewController" bundle:[NSBundle mainBundle]];
singleViewController.prova.text=#"CIAO";
singleViewController.ciao=#"sample";
[singleViewController.web loadHTMLString:item.summary baseURL:nil];
NSLog(#"conternutio:");
NSLog(singleViewController.ciao);
[singleViewController setOpz:#"sample"];
}
I don't see any instantiation of SingleViewController. You have a pointer to it, but I don't see anywhere in your code example where you actually create it.
Somewhere you need something like:
if (!singleViewController) {
singleViewController = [[SingleViewController alloc] init];
}
or
SingleViewController *singleViewController = [[SingleViewController alloc] initWithNibName:#"SingleView" bundle:nil];
or from somewhere else you need to send a message or set an instance variable in NewsViewController that points to an existing instance of SingleViewController.

iOS app use sudzc to connect soap web service

I'm doing a simple change password through a web service project, using sudzc to generate the SOAP requests.
When I run the project, I just keep receiving nothing in the simulator.
Here's my code:
.h
#import <UIKit/UIKit.h>
#interface ChangePasswordViewController : UIViewController {
NSString *CPstr1;
NSString *CPstr2;
NSString *CPstr3;
IBOutlet UITextField *CPText1;
IBOutlet UITextField *CPText2;
IBOutlet UITextField *CPText3;
}
#property (nonatomic,retain) IBOutlet UITextField *CPText1;
#property (nonatomic,retain) IBOutlet UITextField *CPText2;
#property (nonatomic,retain) IBOutlet UITextField *CPText3;
#property (nonatomic,retain) IBOutlet UITextView *CPResult;
-(IBAction) CPSendString;
#end
viewcontroller.m
#import "ChangePasswordViewController.h"
#import "MINEHBJTService.h"
#implementation ChangePasswordViewController
#synthesize CPText1,CPText2,CPText3;
#synthesize CPResult;
//to send the user's old and new password to CPstr
-(IBAction) CPSendString{
CPstr1 = [[NSMutableString alloc] initWithString:CPText1.text];
CPstr2 = [[NSMutableString alloc] initWithString:CPText2.text];
CPstr3 = [[NSMutableString alloc] initWithString:CPText3.text];
MINEHBJTService *service = [[MINEHBJTService alloc] init];
[service ChangePassword: self action: #selector(handleChangePassword:)
sUserID:CPstr1 sPassWord:CPstr2 sNewPassword:CPstr3];
return;
}
-(void) hadleChanePassword:(int)value{
int result = value;
if(result==1){
CPResult.text = #"Change password sucessed";
}
else {
CPResult.text = #"Change password failed";
}
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[CPstr1 release];
[CPstr2 release];
[CPstr3 release];
CPResult.text = #"";
[ MINEHBJTService release];
[super dealloc];
}
#end
In the service call you define the callback method as handleChangePassword. However, the actual implementation of this method is mispelled as hadleChanePassword.
Fix the naming and you should start seeing something in the output field in the simulator.

Having problem with view switching from a subview

Maybe this is a silly question but I'm having a problem with switching the view in a subview. Let me explain the code structure:
I have these class files:
/classes/MySoftwareAppDelegate.h
/classes/MySoftwareAppDelegate.m
/classes/ViewController.h
/classes/ViewController.m
/classes/LoginController.h
/classes/LoginController.m
/classes/CustomersController.h
/classes/CustomersController.m
I have these views:
/resources/MainWindow.xib
/resources/Login.xib
/resources/Customers.xib
In the AppDelegate, I have successfully inserted the sub view "Login" and it's displayed whenever the app starts.
In the login view, I enter my username and password and then click the "Login" button. When this button is clicked, an IBAction is triggered. In this IBAction, I want to change the current subview with the Customers.
Here's the code I have used:
MySoftwareAppDelegate.h
#import <UIKit/UIKit.h>
#class ViewController;
#interface MySoftwareAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ViewController *viewController;
#end
MySoftwareAppDelegate.m
#import "MySoftwareAppDelegate.h"
#import "ViewController.h"
#implementation MySoftwareAppDelegate
#synthesize window;
#synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#class LoginController;
#interface ViewController : UIViewController {
LoginController *loginController;
}
#property (nonatomic, retain) LoginController *loginController;
#end
ViewController.m
#import "ViewController.h"
#import "LoginController.h"
#implementation ViewController
#synthesize loginController;
- (void)viewDidLoad {
LoginController *tmpViewController = [[LoginController alloc] initWithNibName:#"Login" bundle:nil];
self.loginController = tmpViewController;
[self.view insertSubview:loginController.view atIndex:0];
[tmpViewController release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
if (self.loginController.view.superview == nil) {
self.loginController = nil;
}
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[loginController release];
[super dealloc];
}
#end
LoginController.h
#import <UIKit/UIKit.h>
#class CustomersController;
#interface LoginController : UIViewController {
UIButton *loginButton;
UITextField *usernameTextField;
UITextField *passwordTextField;
NSMutableString *available_credits;
NSString *current_xml_element;
CustomersController *customersController;
}
#property (nonatomic, retain) IBOutlet UIButton *loginButton;
#property (nonatomic, retain) IBOutlet UITextField *usernameTextField;
#property (nonatomic, retain) IBOutlet UITextField *passwordTextField;
#property (nonatomic, retain) NSMutableString *available_credits;
#property (nonatomic, retain) NSString *current_xml_element;
#property (nonatomic, retain) CustomersController *customersController;
-(IBAction)textFieldDoneEditing:(id)sender;
-(IBAction)backgroundTap:(id)sender;
-(IBAction)loginToAccount:(id)sender;
#end
LoginController.m
#import "LoginController.h"
#import "CustomersController.h"
#implementation LoginController
#synthesize loginButton;
#synthesize usernameTextField;
#synthesize passwordTextField;
#synthesize customersController;
- (void)viewDidLoad {
UIImage *buttonImageNormal = [UIImage imageNamed:#"whiteButton.png"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
UIImage *buttonImagePressed = [UIImage imageNamed:#"blueButton.png"];
UIImage *stretchableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[loginButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
[loginButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[usernameTextField release];
[passwordTextField release];
[super dealloc];
}
-(IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
-(IBAction)backgroundTap:(id)sender {
[usernameTextField resignFirstResponder];
[passwordTextField resignFirstResponder];
}
-(IBAction)loginToAccount:(id)sender {
// bla bla bla... Login check process is done here
CustomersController *tmpViewController = [[CustomersController alloc] initWithNibName:#"Customers" bundle:nil];
self.customersController = tmpViewController;
[self.view removeFromSuperview];
[tmpViewController release];
}
#end
As you can see above, in LoginController.m's loginToAccount method, I am checking the login info and then setting the new view controller for the "customers" sub-view.
Then I am removing the current "Login" subview from the super view but don't know how to add the new "Customers" sub view.
In MainWindow.xib, I have one view controller which is linked to ViewController class and it's the root contoller.
Any help is appreciated. Because I am new to Objective-C and iPhone programming, please do your best to explain considering a novice programmer :)
Thanks again.
You are not adding any views to the view hierarchy, just removing the login view controller. If you want to add your customer view to the view hierarchy you should use:
CustomersController *tmpViewController = [[CustomersController alloc] initWithNibName:#"Customers" bundle:nil];
self.customersController = tmpViewController;
[self presentModalViewController:tmpViewController]
The above method will make sure that the viewWillLoad, didLoad etc are called on the account view controller. It will also take care of removing and adding the correct views to the view hierarchy for you.