0

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 :)

Eshwar Chaitanya
  • 942
  • 2
  • 13
  • 34

1 Answers1

1

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

john
  • 3,043
  • 5
  • 27
  • 48
  • Sorry I took int only,by mistake I posted,I edited the post,see but its not working Thanks for the reply – Eshwar Chaitanya Dec 16 '11 at 08:01
  • Oh,thanks for the reply...I will get back to you after implementation – Eshwar Chaitanya Dec 16 '11 at 08:28
  • awfullyjohn Sorry to say it didn't worked properly,actually when I click save,as u said I wrote ++self.numberOfSaves and when tried to display in console NSLog(@"Number of saves = %d",self.numberOfSaves); Its displaying Number of saves = 0,I also initiated numberOfSaves = 0; but without mentioning also it is displaying same res.. what was my wrong step please guide me...Thanks in advance :) – Eshwar Chaitanya Dec 16 '11 at 10:04
  • Find out if `- (IBAction)save:(id)sender` is getting called. Put an `NSLog()` there. If not, make sure its connected to the button. – john Dec 16 '11 at 10:11
  • awfullyjohn Yeah,thanks for the reply once again,every thing's fine,I have placed a break point and checked it out,more over it is not an user defined button,It is right bar button in navigation bar,and for that action is written,the same problem,I will edit the question by placing all the code,....please help me out Thanks for the patience and response :) – Eshwar Chaitanya Dec 16 '11 at 10:23
  • awfullyjohn Actually my requirement is different,this is the task I need to achieve for my project...please go through the following link http://stackoverflow.com/questions/8475446/generating-cells-automatically-to-display-saved-reminder-values-of-database-in-t For achieving that I want to count the number of saves...so that I can generate the number of cells using or taking the value as reference .... I will be very glad and thankful to you If you can suggest me a way to achieve it...Waiting for your reply..Thanks in advance :) – Eshwar Chaitanya Dec 17 '11 at 07:58