简体   繁体   中英

I want to save UISegmentedControl.selectedSegmentIndex in Core Data - but how

I am using a segmented control as a kind of radio button system for a contact app. There is three buttons, and the questions is if the contact is an existing customer. The names of the buttons are "No", "Yes", "Other Division"

I am absolutely green in developing iOS, and this one is pulling my last remaining grey hairs out

I want to be able to save the selectedSegmentIndex value in the db of Core Data, so i can show the status on the next view correctly - but i am really struggling to get my head around, how to save the value:

if (self.existCustSelection.selectedSegmentIndex == 2)
    [self.currentContact setExistCust:[existCustSelection stringWithFormat:@"%d", @"2"]];

-This gives a compile error for

No visible @Interface for 'UISegmentedControl' declares the selector 'stringWithFormat:'

I do understand that - my issue is, i want to set the existCust instead of existCustSelection , but for whatever reason, i don't get that option during writing the code.

My read goes like this and gives no error or warning:

if ([currentContact existCust] == @"2") {
            self.existCustSelection.selectedSegmentIndex = 2;
    }

Hopefully, there is a great solution out there? :)

Thanks in advance, Mickey

Does existCust have to be a string? If it only ever stores numbers, consider using NSNumber :

NSNumber* custNumber = [NSNumber numberWithInteger:existCustSelection.selectedSegmentIndex];

If you definitely do need a string, here's how to convert your index to a string using stringWithFormat:

NSString* custString = [NSString stringWithFormat:@"%d", existCustSelection.selectedSegmentIndex];

There are two problems with your call to stringWithFormat :

  1. stringWithFormat is a class method of NSString . You're asking your instance of UISegmentedControl to return a string, and it doesn't know how to do that.
  2. Having requested a %d in stringWithFormat, the first argument needs to be an integer, such as 2 , not the string @"2" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM