PDA

View Full Version : passing SelectedSegment selection




santelia
Aug 24, 2008, 12:36 PM
Trying to pass the selection of a SegmentedControl to a variable, but get some difficulties.

my ViewController.h says:

@interface testViewController : UIViewController <UITextFieldDelegate>{
IBOutlet UITextField *myname;
IBOutlet UILabel * actiontodo;
NSString * mystring;
NSString * myindex;
}

....... some properties .......
@property (nonatomic, retain) UITextField *myname;
@property (nonatomic, retain) UILabel *actiontodo;
@property (nonatomic, copy) NSString *mystring;
@property (nonatomic) NSInteger selectedSegmentIndex;

- (IBAction)myaction:(id)sender;
@end


and my ViewController.m says:

#import "testViewController.h"

@implementation testViewController

@synthesize actiontodo,myname,mystring, myindex;

- (IBAction)myaction:(id)sender {
NSString *myname_string;

indice = self.SelectedSegmentIndex;

if ([myindex] = 0) {
myname_string = @"aaa";
}else{
if ([myindex] = 1) {
myname_string = @"bbb";
}
}

self.mystring = [[NSString alloc] initWithFormat:@"Action required %@!", myname_string];
self.actiontodo.text = self.mystring;
}


I get some warnings and errors, such as " warning: unused variable 'myname_string' " and " error: request for member 'SelectedSegmentIndex' in something not a structure or union ", plus some warnings after the "end" tag about the definition of a method for 'SelectedSegmentIndex'.

Any help?



robbieduncan
Aug 24, 2008, 01:01 PM
Whilst I cannot really work out what you are trying to do the errors are very simple. The compiler is telling you what is wrong. You need to learn to read these errors and understand them as you're going to be seeing lost of them.

1) warning: unused variable 'myname_string'

You have declared a variable called myname_string and not used it anywhere. This is a warning: it won't stop the code compiling but is often an indication that you've made a mistake somewhere (as you'd not declare a variable you didn't intend to use).

2) error: request for member 'SelectedSegmentIndex' in something not a structure or union

You have tried to access SelectedSegmentIndex as a member of a structure or union and it isn't. In this case the error is a little confusing as you don't necessarily think of properties of objects like this. The error is telling you that you are trying to access a property that doesn't exist in your class. The reason is that you have declared the property as selectedSegmentIndex and then tried to access it as SelectedSegmentIndex. These are not the same: every character must match including case. You need to access it as self. selectedSegmentIndex;

santelia
Aug 24, 2008, 01:34 PM
Thank you Rob for such a quick reply.

1) warning: unused variable 'myname_string'

You have declared a variable called myname_string and not used it anywhere.

Isn' t it a use where few row after I've coded like this?

if ([myindex] = 0) {
myname_string = @"aaa";
}else{
if ([myindex] = 1) {
myname_string = @"bbb";
}
}

Am perhaps I forgetting any obvious thing?

The reason is that you have declared the property as selectedSegmentIndex and then tried to access it as SelectedSegmentIndex
Uh, luckily it was only a typo... thank you for your eagle' sight!

santelia
Aug 24, 2008, 01:39 PM
The debugger also display an error here;

- (IBAction)myaction:(id)sender {
NSString *myname_string;

indice = self.SelectedSegmentIndex;

if ([myindex] = 0) {

error: syntax error before ']' token

myname_string = @"aaa";
}else{
if ([myindex] = 1) {
myname_string = @"bbb";
}
}

self.mystring = [[NSString alloc] initWithFormat:@"Action required %@!", myname_string];
self.actiontodo.text = self.mystring;
}

Can't figure out the solution.

robbieduncan
Aug 24, 2008, 01:40 PM
For whatever reason the compiler isn't seeing the usage of it. Not sure why: it should be.

I would note that your comparisons are all wrong too:

= : assign
== : comparative test

Also [myindex] is the syntax to send a message to myindex, but not selector is provided. I imagine this should be causing compiler warnings too.

You want

if (myindex == 0)


This is a basic C (not even Objective-C) error. I suggest you spend some time learning the language basics before actually writing any code.

This is a pretty good place to start (http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/chapter_1_section_1.html). Read it all. If you don't understand something don't skip it: work out what's going on.

santelia
Aug 25, 2008, 03:10 AM
Thanks again, Rob.

I'll make good use of your suggestions.

You're right, some more learning of C would be better... ;)

santelia
Aug 25, 2008, 04:59 AM
P.S.

I had two Discovery: first and second serie. The ones with the 4 cyl engine. Slow but great. Lot of fun going in wonderful places where only Lands can go.
;)

chuck.han
Sep 11, 2008, 08:18 AM
- (IBAction) myaction: (id) sender {
NSString *myname_string;

indice = self.SelectedSegmentIndex;

// ...
}

It should be:

indice = ((UISegmentedControl*) sender).selectedSegmentIndex;

The property SelectedSegmentIndex (wrong spelling anyway) doesn't live on self.

Cheers, Chuck