How to access or call an action in to a function - iphone

I have the right navigation bar button action,I have set an action for it like the following:
-(IBAction)save:(id)sender
{
//Code for saving entered data
UITextField *fieldOne = [self.fields objectAtIndex:0];
UITextField *fieldTwo = [self.fields objectAtIndex:1];
UITextField *fieldThree = [self.fields objectAtIndex:2];
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &remindersDB) == SQLITE_OK && textField.text != nil)
{
NSString *insertSQL = [NSString stringWithFormat:#"INSERT INTO reminders(name, event, date) VALUES (\"%#\", \"%#\", \"%#\")", fieldOne.text, fieldTwo.text,fieldThree.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(remindersDB, insert_stmt, -1, &statement, NULL);
NSLog(#"%#",fieldOne.text);
NSLog(#"%#",fieldTwo.text);
NSLog(#"%#",fieldThree.text);
if (sqlite3_step(statement) == SQLITE_DONE)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"\n Reminder Saved." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
[alert dismissWithClickedButtonIndex:0 animated:YES];
[alert release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Reminder not saved" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
rowID = sqlite3_last_insert_rowid(remindersDB);
NSLog(#"last inserted rowId = %d",rowID);
sqlite3_finalize(statement);
sqlite3_close(remindersDB);
}
//Alert for not entering any data
if ([textField.text length] == 0)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Fill Data" message:#"Please fill name,event and date of reminder" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
++self.numberOfSaves;
NSLog(#"Number of saves = %d",numberOfSaves);
[self.tableView reloadData];
}
Now I want to check how many times the button is clicked,I have an idea in mind that to create a function and used the condition
-(int)numberOfSaves
{
int saves = 0;
if(self.navigationItem.rightBarButtonItem.enabled==YES)
{
saves++;
}
return saves;
}
It's not working......
Is there anyway to check such condition,If so how can I achieve it,I am newbie to objective-c,please help me out...
Thanks all in advance :)

You cannot return from a void method.
You'd want to change -(void)numberOfSaves to -(int)numberOfSaves, if that were the right way to go about things, but I don't think this is what you want.
In your header file (.h) you'll want to declare an instance variable for the class:
#property (nonatomic, strong) NSInteger numberOfSaves;
In your implementation file (.m) make sure to synthesize it:
#synthesize numberOfSaves = _numberOfSaves;
Replace your IBAction method:
- (IBAction)save:(id)sender
{
++self.numberOfSaves;
}
And just throw away the -(void)numberOfSaves method all together. This should accomplish what I think you're trying to do. You can figure out how many saves you have committed now with self.numberOfSaves

Related

How to compare my email text field value to database value in iphone

I have a problem in comparing my textfield email value to the value in sqlite database. If both the values are same then it shows alertbox that "email already exist in database". I have created a function for this purpose named findcontact, I am calling the function from button click. Button click method is working because when I click the button then buttonclick method generate the log which I have written in findContact method.
But problem for me is that how to compare both the email and show alert box if both email matches
- (void) findContact:(NSString *) email{
const char *dbpath = [path UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK){
NSString *querySQL = [NSString stringWithFormat: #"SELECT email FROM CONTACT WHERE email=\"%#\"",email];
NSLog(#" email = %#", querySQL);
const char *query_stmt = [querySQL UTF8String];
NSLog(#" char email = %s", query_stmt);
if (sqlite3_prepare_v2(database, query_stmt, -1, &init_statement, NULL) == SQLITE_OK){
if (sqlite3_step(init_statement) == SQLITE_ROW){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"email already exist in database" delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK ", nil];
[alert show];
[alert release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Success" message:#"email not exist" delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK ", nil];
[alert show];
[alert release];
}
sqlite3_finalize(init_statement);
}
sqlite3_close(database);
}
}
Do comparison like this in your code
while(sqlite3_step(compiledStatement)==SQLITE_ROW)
{
NSString *emaiID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,0)];
if ([emaiID isEqualToString:yourTextField.text])
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"email already exist in database"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"OK ", nil];
[alert show];
}
else{
//Your task
}
}
Check this
NSString *querySQL = [NSString stringWithFormat: #"SELECT email FROM CONTACT WHERE email='%#'",email];
if(sqlite3_prepare_v2(sharedDataBase, [querySQL UTF8String], -1, &statment, nil) == SQLITE_OK)
{
if(sqlite3_step(statment)==SQLITE_ROW){
char *email=(char *)sqlite3_column_text(statment, 0);
NSString *emailStr =[NSString stringWithUTF8String:email];
// email exist
}
else{
// email not exist
}
sqlite3_finalize(statment);
}
else
sqlite3_close(sharedDataBase);

Checking Username and Password to match in SQLIte

How can I check if the Username and Password would be the same as the Username and Password in the Database.
AppDelegate.m:
-(void) readLoginFromDatabase {
// Setup the database object
sqlite3 *database;
// Init the Array
lo = [[NSMutableArray alloc] init];
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "Select * from Login";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *aLoginName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aLoginPass = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *aLoginAccess = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
// Create a new object with the data from the database
Login *los = [[Login alloc] initWithName:aLoginName pass:aLoginPass access:aLoginAccess];
// Add the object to the Array
[lo addObject:los];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
ViewController.m:
-(IBAction)buttonWasClicked:(id)sender
{
if ((userText.text != logins.loginUser) && (passText.text != logins.loginPass))
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR" message:[[NSString alloc] initWithFormat:#"Your Username/Password is incorrect!"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else
{
StudentViewController *studentView = [self.storyboard instantiateViewControllerWithIdentifier:#"studentView"];
[studentView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:studentView animated:YES completion:nil];
}
if (userText.text != logins.loginUser)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR" message:[[NSString alloc] initWithFormat:#"Your Username is incorrect!"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
if (passText.text != logins.loginPass)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR" message:[[NSString alloc] initWithFormat:#"Your Password is incorrect!"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
Even when the Username and Password I type is exactly the same as in the database, it would still show me the error message instead of displaying the next view.
You have to check NSStrings in objective-c with
isEqualToString
For your example:
if (![userText.text isEqualToString:logins.loginUser])
Check: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/isEqualToString:
You can use isEqualToString method to check that two string is equal....
if ([userText.text isEqualToString:logins.loginUser])
and apply condition like this
-(IBAction)buttonWasClicked:(id)sender
{
Login *logins = nil;
logins= [lo objectAtIndexPath:0];
if ([userText.text isEqualToString:logins.loginUser] && [passText.text isEqualToString:logins.loginPass])
{
StudentViewController *studentView = [self.storyboard instantiateViewControllerWithIdentifier:#"studentView"];
[studentView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:studentView animated:YES completion:nil];
}
else if (![userText.text isEqualToString:logins.loginUser])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR" message:[[NSString alloc] initWithFormat:#"Your Username is incorrect!"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else if (![passText.text isEqualToString:logins.loginPass])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR" message:[[NSString alloc] initWithFormat:#"Your Password is incorrect!"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR" message:[[NSString alloc] initWithFormat:#"Your Username/Password is incorrect!"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}

invalide username or password when validate it sqlite iphone

when Click on update button then data does not updated i want to just updated my password
first match old password
if old password match with database then validate new password and conform password using isEquelToString then click on update button and oldpassword must be updated with newPassword
I think user name and password match for single user only there for if it match then it will return one row using following code. Check the following code i think this will work for you.
-(void)checkingDatabase{
databaseName = #"trylogin.sqlite";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [[documentsDir stringByAppendingPathComponent:databaseName]retain];
NSLog(#"databse path %#",databasePath);
database = nil;
int myInt = 0;
if(sqlite3_open([databasePath UTF8String], &database) ==SQLITE_OK){
NSString *sql = [[NSString alloc] initWithFormat:#"select COUNT(*) from loginTry where userName='%#' and password='%#'",txtusername.text,txtPassword.text];
const char *sqlStatement = [tempSQL cStringUsingEncoding:NSUTF8StringEncoding];
[tempSQL release];
sqlite3_stmt *compiledStatement;
sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);
if(sqlite3_step(compiledStatement) == SQLITE_ROW) {
myInt = (int)sqlite3_column_int(compiledStatement, 0);
}
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
}
if(myInt ! = 0){
AnimalViewController *FBController = [[AnimalViewController alloc] initWithNibName:#"AnimalViewController" bundle:nil];
[self.view addSubview:FBController.view];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"UIAlertView" message:#"Wrong username or password" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
As above same but some change at condition checking as follows..
if(myInt ! = 0) {
if([newpassword.text isEqualToString: confirmPassword.text]) {
//Write code for update query.
}
else {
//print message new password and confirm password does not match.
}
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"UIAlertView" message:#"Wrong username or password" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert show];
[alert release];
}

how to updtae Sqlite last insert row id in iphone?

I am making an application where i want to use update query?
In my application i have two stages to complete the registration process
in page1 i have registration form and one submit button?
on click of submit button all the details should get insert into my sqlite table.
in page2 i have confirmation page and one edit and continue button?
all the value saved in a table should be view by the user on this page and if he want's to make any change he should be able to do that. once user had edited some value and he press continue button all the value insert should get updated?
but i have try doing this but i am not able to update the last enter value?
following is my code page1 insert code:
-(void)submit
{
if( ([UserName.text isEqualToString:#""]) || ([Password.text isEqualToString:#""]) ||
([ConfirmPassword.text isEqualToString:#""]) || ([Name.text isEqualToString:#""]) ||
([Email.text isEqualToString:#""]) || ([ContactNO.text isEqualToString:#""]) ||
([MobileNo.text isEqualToString:#""]) || ([Address.text isEqualToString:#""]) )
{
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Error!!"
message:#"Please fill in the details." delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
}
else
{
Confirmation_form *conForm = [[Confirmation_form alloc] initWithNibName:#"Confirmation_form" bundle:nil];
conForm.data = UserName.text;
conForm.data1 = Password.text;
conForm.data2 = ConfirmPassword.text;
conForm.data3 = Name.text;
conForm.data4 = Email.text;
conForm.data5 = ContactNO.text;
conForm.data6 = MobileNo.text;
conForm.data7 = Address.text;
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &test1DB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:#"INSERT INTO test(UserName, Password, ConfirmPassword, Name, Email, ContactNO, MobileNo, Address) VALUES (\"%#\", \"%#\", \"%#\", \"%#\", \"%#\", \"%#\", \"%#\", \"%#\")",UserName.text, Password.text, ConfirmPassword.text, Name.text, Email.text, ContactNO.text, MobileNo.text, Address.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(test1DB, insert_stmt, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
//status.text = #"Contact added";
UserName.text = #"";
Password.text = #"";
ConfirmPassword.text = #"";
Name.text = #"";
Email.text = #"";
ContactNO.text = #"";
MobileNo.text = #"";
Address.text = #"";
sqlite3_last_insert_rowid;
rowID = sqlite3_last_insert_rowid(test1DB);
NSLog(#"last inserted rowId = %d",rowID);
sqlite3_reset(statement);
sqlite3_finalize(statement);
sqlite3_close(test1DB);
[self.navigationController pushViewController:conForm animated:YES];
}
}
}
}
from the above code i am able to get the row id but i am not able to use that rowid in update query
following is my code page2 Update code:
-(IBAction)Update;
{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &test1DB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:#"UPDATE test SET UserName='%#',Password='%#',ConfirmPassword='%#',Name='%#',Email='%#',ContactNO='%#',MobileNO='%#',Address='%#'WHERE ID='%#'",UserName.text,Password.text,ConfirmPassword.text, Name.text, Email.text, ContactNO.text, MobileNo.text, Address.text, sqlite3_last_insert_rowid(test1DB)];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(test1DB, insert_stmt, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
sqlite3_step(statement);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"UIAlertView" message:#"Record added" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert = nil;
sqlite3_finalize(statement);
sqlite3_close(test1DB);
}
else {
sqlite3_step(statement);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"UIAlertView" message:#"Record notadded" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert = nil;
}
}
Simple Way -> create "someconstant.h" file declare your _rowID variable there.
Import file in your class,assign your value in first class and used it in second class.

UIAlert View-for yes/no condition

my app needs alert msg and if yes button pressed then one more alert msg and then i have to called a method.This is my code:
-(IBAction)resetPressed:(id)sender
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag ==1)
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
alert.tag =2;
[alert show];
[alert release];
}
else if(alertView.tag ==2)
{
[self resetArray];
}
}
Thanks.
I'm not sure what your goal is but a few things look wrong to me anyways:
First of all you should create your strings this way:
NSString *title= #"Warning";
There's no need to use stringWithFormat in your case.
Then, it doesn't seem you properly set the first UIAlert's tag to 1, and the default value for tags is 0 so I guess the if statements in didDismissWithButtonIndex are never true.
Also, you should check which button was pressed using buttonIndex, otherwise you are going to show both alert and call [self resetArray] whichever button is pressed by the user.
Hope that helps.
In your code, you create the first alert, but never actually set the tag on it. You should do:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
alert.tag = 1; //Or 2, or something.
[alert show];
[alert release];
Then the code in your delegate method will run.
Please define two separate UIAlertView in .h file
#interface XYZViewController:UIViewController
{
UIAlertView *firstAlertView;
UIAlertView *secondAlertView;
}
Now in your .m file modify as below:
-(IBAction)resetPressed:(id)sender
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
if(firstAlertView == nil)
{
firstAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
}
[firstAlertView show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView == firstAlertView)
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
if(secondAlertView == nil)
{
secondAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
}
[secondAlertView show];
}
else if(alertView == secondAlertView)
{
[self resetArray];
}
}
and in dealloc method please release the allocated UIAlertviews.
Hope i am clear to you.
Thanks,
Jim.