jwknz
jwknz

Reputation: 6824

ios - UISegmentedControl and NSString

Hello I have the following code:

if(_deviceSegmentCntrl.selectedSegmentIndex == 0)
{
    deviceOne = [[NSString alloc] initWithFormat:@"string01"];
}
if(_deviceSegmentCntrl.selectedSegmentIndex == 1)
{
    deviceOne = [[NSString alloc] initWithFormat:@"string02"];
}
if(_deviceSegmentCntrl.selectedSegmentIndex == 2)
{
    deviceOne = [[NSString alloc] initWithFormat:@"string03"];
}

NSString *deviceType = [[NSString alloc] initWithFormat:_deviceSegmentCntrl];

I am wanting to have the NSString output the string the user selects in the segmented control.

How do I append the initWithFormat: so that it reflects the chosen index?

Cheers

Upvotes: 0

Views: 431

Answers (3)

Eitan
Eitan

Reputation: 1348

It sounds like you want the following:

int myIndex = _deviceSegmentCntrl.selectedSegmentIndex;
NSString *deviceType = [[NSString alloc] initWithFormat:@"%d",myIndex];

Assuming your segmented control had only three options - deviceType will be a NSString variable for the string "0", "1", or "2" according to the selected index.

Make sure that you release deviceType when you are done using it.

Editing according to your comment -

If you want to include the deviceOne string in your deviceType string, then you can do the following:

NSString *deviceType = [[NSString alloc] initWithFormat:@"%@",deviceOne];

Alternatively, if you want the text from the segmented control button that was chosen, then the following will help you.

int myIndex = _deviceSegmentCntrl.selectedSegmentIndex;
NSString *segmentString = [_deviceSegmentCntrl titleForSegmentAtIndex:myIndex];
NSString *deviceType = [[NSString alloc] initWithFormat:@"%@",segmentString];

Upvotes: 0

Dima
Dima

Reputation: 23634

if(_deviceSegmentCntrl.selectedSegmentIndex == 0)
{
    deviceOne = [[NSString alloc] initWithFormat:@"string01"];
}
if(_deviceSegmentCntrl.selectedSegmentIndex == 1)
{
    deviceOne = [[NSString alloc] initWithFormat:@"string02"];
}
if(_deviceSegmentCntrl.selectedSegmentIndex == 2)
{
    deviceOne = [[NSString alloc] initWithFormat:@"string03"];
}

NSString *deviceType = deviceOne;

is this what you're trying to do? Just display the string you created? Why do you even need the deviceType string if that is the case?

Upvotes: 0

Chris Trahey
Chris Trahey

Reputation: 18290

Firstly, NSString's are so common that there is a shorthand to greatly simplify your instantiations:

deviceOne = @"string01";

is has an identical result to

deviceOne = [[NSString alloc] initWithFormat:@"string01"];

Now, I'm not sure exactly what you are wanting to achieve, so I'll mention a few things that I think are in the ballpark.

If you just want a string that is exactly what is displayed on the control, use

NSString deviceType = [_deviceSegmentCntrl titleForSegmentAtIndex:_deviceSegmentCn.selectedSegmentIndex];

When you want a more complex string, you can use the class method stringWithFormat, perhaps like this:

NSString *deviceType = [NSString stringWithFormat:@"%@ - %@", deviceOne, [_deviceSegmentCntrl titleForSegmentAtIndex:_deviceSegmentCn.selectedSegmentIndex]];

Where deviceOne is from your posted code.

Upvotes: 2

Related Questions