Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

santelia

macrumors newbie
Original poster
Aug 17, 2008
13
0
Trying to pass the selection of a SegmentedControl to a variable, but get some difficulties.

my ViewController.h says:

Code:
@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:

Code:
#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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors newbie
Original poster
Aug 17, 2008
13
0
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?

Code:
if ([myindex] = 0) {
		[COLOR="Red"]myname_string[/COLOR] = @"aaa";
	}else{
		if ([myindex] = 1) {
			[COLOR="red"]myname_string[/COLOR] = @"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

macrumors newbie
Original poster
Aug 17, 2008
13
0
The debugger also display an error here;

Code:
- (IBAction)myaction:(id)sender {
	NSString *myname_string;
	
	indice = self.SelectedSegmentIndex;
	
	if ([myindex] = 0) {

[COLOR="Red"][B]error: syntax error before ']' token[/B][/COLOR]

		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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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
Code:
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. Read it all. If you don't understand something don't skip it: work out what's going on.
 

santelia

macrumors newbie
Original poster
Aug 17, 2008
13
0
Thanks again, Rob.

I'll make good use of your suggestions.

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

santelia

macrumors newbie
Original poster
Aug 17, 2008
13
0
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

macrumors newbie
Jul 29, 2008
2
0
- (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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.