Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7654135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:12:04+00:00 2026-05-31T12:12:04+00:00

I have a form contains text fields,a textView and 2 switches.Now I am using

  • 0

I have a form contains text fields,a textView and 2 switches.Now I am using sqlite database for saving data in to database.Every thing is fine with text fields,text view saving,retrieving and even displaying the saved.As we know we don’t have any feature called check box,we need to make use of switch which behaves/acts as that of check box functionality.Now while saving the switch state in to the database.I am saving it as a string/text format only,i.e. check the state of switch,assign value as ‘1’ for ON and ‘0’ for OFF to an integer.Now you might get a bit confused how can I save the integer to string format,yes you are right!I am converting the int value in to number and finally number to string.Then I have formed the query and have inserted the values in to sqlite database table.
Here is the code snippet of it for clear understanding:

int smsValue = 0;

    if ([smsSwitch isOn])
    {
        smsValue = 1;
    }

    else
    {
        smsValue = 0;
    }

    NSNumber *smsNum = [NSNumber numberWithInt:smsValue];
    NSString *selectedVal = [smsNum stringValue];

The selectedVal is what we are going to pass as string while forming the query,I also have found out that the state is getting saved properly,i.e. the value is correct.

Now I have a requirement,in my form I have switch for sms,Initially the state of that switch is Off,during off state,the last 2 fields i.e. textField and textView are hidden.If the user selects it (i.e. state is ON),then both the fields are said to be open to enter the values.

Totally I have saved 3 reminders,out of which I have saved 2 with out sms and 1 with sms and both the fields(last) filled.Now to pass the data,or to keep track of record saved after filling the form I have used a model class,please see the following snippet for clear understanding…how I have passed values for other fields(textFields):

-(void)viewDidLoad
{
    textField.text = reminderInstance.Name;
    textField1.text = reminderInstance.Event;
    textField2.text = reminderInstance.Date;
    textField3.text = reminderInstance.numDays;
    textField4.text = reminderInstance.remGroup;

    [super viewDidLoad];
}

Here the reminderInstance is object of the model class holding values like name,event,date etc…Now last 2 fields i.e. textField5 and textView are tied or linked to state of sms switch.If it is selected we need to consider both the fields values and pass them for user to edit/make changes.If not we need not bother about the last 2 fields.

Here is the action for the switch:

-(IBAction)toggleEnabledForswitch:(id)sender
{
    if(smsSwitch.on)
    {
        textField5.hidden = NO;
        textView.hidden = NO;
    }

    else 
    {
        textField5.hidden = YES;
        textView.hidden = YES;
    }
}

Now in save action,I am checking the state of the switch and saving the values accordingly,the following code snippet will show a clear picture:

-(IBAction)save:(id)sender
{
int smsValue = 0;

    if ([smsSwitch isOn])
    {
        smsValue = 1;
    }

    else
    {
        smsValue = 0;
    }

    NSNumber *smsNum = [NSNumber numberWithInt:smsValue];
    NSString *selectedVal = [smsNum stringValue];

    reminderInstance.smsString = selectedVal;
sqlite3_stmt *statement = nil;
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &remindersDB) == SQLITE_OK && textField.text != nil)
    {
      if (self.navigationItem.rightBarButtonItem.title == @"Save" && [textField.text length] !=0 && [textField1.text length] !=0 && [textField2.text length] !=0 && [textField3.text length] !=0 && [textField4.text length] !=0)
       {
            NSLog(@"am in the save loop");

            if (smsValue == 0)
            {
                NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO reminders(name,event,date,bfr,grp,val,sms) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\")", textField.text, textField1.text,textField2.text,textField3.text,textField4.text,isSelected,selectedVal]; 
                const char *insert_stmt = [insertSQL UTF8String];
                sqlite3_prepare_v2(remindersDB, insert_stmt, -1, &statement, NULL);
            }

            else if (smsValue == 1)
            {
                NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO reminders(name,event,date,bfr,grp,val,sms,num,bod) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\")", textField.text, textField1.text,textField2.text,textField3.text,textField4.text,isSelected,selectedVal,textField5.text,textView.text]; 
                textField5.text = reminderInstance.number;
                textView.text = reminderInstance.msgBody;
                const char *insert_stmt = [insertSQL UTF8String];
                sqlite3_prepare_v2(remindersDB, insert_stmt, -1, &statement, NULL);
            }

            if (sqlite3_step(statement) == SQLITE_DONE)
            {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"\nReminder Saved" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil,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];
            }

        }
    }
 }

I took another view controller with table view where I have populated a table view to view all the records that are being saved and upon selection we take him/her to the controller page that contains form i.e. populated with all the values.So far I have been successful in passing all the values using:

1.The following code in form controller page:

-(id) initWithReminder:(ReminderClass *)aReminder 
{
    if ( (self=[super init]) ) 
    {
        self.reminderInstance = aReminder;
    }

    return self;
}

2.The following parts of code in display controller:

-(void)loadReminders
{

    // setup the reminders array
    self.array = [[NSMutableArray alloc] init];

    //Retrieve the values of database
    const char *dbpath = [self.databasePath UTF8String];
    sqlite3_stmt *statement;
    if (sqlite3_open(dbpath, &remindersDB) == SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM reminders"];

        const char *query_stmt = [querySQL UTF8String];

        if (sqlite3_prepare_v2(self.remindersDB ,query_stmt , -1, &statement, NULL) == SQLITE_OK)
        {
            while (sqlite3_step(statement) == SQLITE_ROW)
            {
                ReminderClass *loadedReminder = [[ReminderClass alloc] init];

                loadedReminder.reminderID = sqlite3_column_int(statement, 0);

                loadedReminder.Name = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)]autorelease];                    

                loadedReminder.Event = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)]autorelease];

                loadedReminder.Date = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)]autorelease];

                loadedReminder.numDays = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)]autorelease];

                loadedReminder.remGroup = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)]autorelease];

                loadedReminder.selString = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 6)]autorelease];

                loadedReminder.smsString = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 7)]autorelease];

                NSLog(@"selected value = %@",loadedReminder.smsString);


                 loadedReminder.number = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 8)]autorelease];

                 loadedReminder.msgBody = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 9)]autorelease];


                NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease]; 
                [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
                NSDate *theDate = [dateFormat dateFromString:loadedReminder.Date];
                NSString *stringDate = [dateFormat stringFromDate:theDate];
                loadedReminder.Date = stringDate; 

                NSLog(@"Date = %@",loadedReminder.Date);

                [self.array addObject:loadedReminder];
                [loadedReminder release];
            } 

            sqlite3_finalize(statement);
        }
        sqlite3_close(self.remindersDB);
    }

}

Now what is happening is since I have the table view in display controller with 2 containing fields except that are linked to sms state and one with all the fields filled.When I select the view controller it is crashing at line “loadedReminder.number“:

* Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘* -[NSPlaceholderString initWithUTF8String:]: NULL cString’

when I remove loadedReminder.number and loadedReminder.msgBody,we can view the table(controller page) no crash problem what so ever…Upon selection of table view cell I am doing the following:

-(void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    ReminderClass *rem = [self.array objectAtIndex:indexPath.section];

    // Instantiate your detail/editor view controller,
    // and pass in the ReminderClass object to be edited.
    ERAddReminderViewController *rdvc = [[[ERAddReminderViewController alloc]initWithReminder:rem]autorelease];

    [self.navigationController pushViewController:rdvc animated:YES];
    rdvc.navigationItem.rightBarButtonItem.title = @"Edit";
}

Now I can see all the values properly populated in fields,except the problem with switch.Because it is saving properly,but the switch is always in off state,the state set to default as I have already said during filling the form and saving it (if user selects it and fills the last 2 fields also) to save it(record).

Sorry for this huge description,just wanted to make it detailed,so that any one can understand what my issue is,I have made several searches.But unable to achieve the task.

Can any one please suggest me a way of how to save the switch state in database and then populate the same when loading records from database sqlite.

Thanks every one in advance 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:12:05+00:00Added an answer on May 31, 2026 at 12:12 pm

    I found out the solution for it,i.e. use an NSString and assign string value i.e. the retrieved state values that were added to the array.Let me explain this in detail:

    In Add Reminder page,take an NSString let’s say currentState,now come back to where the saved reminders are viewed,take an array named “stateValues” and add the state values to that array i.e.:

    loadedReminder.smsState = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 7)]autorelease];
    
    [stateValues addObject:loadedReminder.smsState];
    

    Now in did select row at index path,where we are navigating to add reminder page for editing or modifying the existing values of reminder,do this:

    -(void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        ReminderClass *rem = [self.array objectAtIndex:indexPath.section];
        NSLog(@" switch state in did : %@",[stateValues objectAtIndex:indexPath.section]);
     ERAddReminderViewController *rdvc = [[[ERAddReminderViewController alloc]initWithReminder:rem]autorelease];
        rdvc.currentState = [stateValues objectAtIndex:indexPath.section];
    }
    

    We have successfully set the values to current state,now get back to add reminder page to set the state of switch,i.e. in viewDidLoad method do the following:

    -(void)viewDidLoad
    {
    
        if ([currentState isEqualToString:@"0"]) 
        {
            [smsSwitch setOn:NO animated:NO];
        }
        else if([currentState isEqualToString:@"1"])
        {
            [smsSwitch setOn:YES animated:YES];
        }
    }
    

    That’s it,we have now set the state of switch properly by proper access of values that were saved for all consecutive reminders.

    Hope it helps.Thank you!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple data table which contains dynamic form text fields. Each field
I have an asp.net text form that contains numerous decimal fields that are optional.
I have a form which contains a series of fields like: <input type=text name=User[123]
I have a HTML form that contains text fields, checkboxes, radiobuttons and a submit
I have a form which contains 4 text fields when the button is clicked
I have form, the form contains JavaStaticText fields, and Combo box, and Text fields.
I have a ASP.Net web form that contains both text box fields and hidden
I have a form which among others contains text inputs that contain arithmetic data
I have a form which contains a whole heap of data entry fields that
I have an order form with about 30 text fields that contain numerical values.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.