passing SelectedSegment selection

Discussion in 'iPhone/iPad Programming' started by santelia, Aug 24, 2008.

  1. macrumors newbie

    Joined:
    Aug 17, 2008
    #1
    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?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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;
     
  3. thread starter macrumors newbie

    Joined:
    Aug 17, 2008
    #3
    Thank you Rob for such a quick reply.

    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?

    Uh, luckily it was only a typo... thank you for your eagle' sight!
     
  4. thread starter macrumors newbie

    Joined:
    Aug 17, 2008
    #4
    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.
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    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.
     
  6. thread starter macrumors newbie

    Joined:
    Aug 17, 2008
    #6
    Thanks again, Rob.

    I'll make good use of your suggestions.

    You're right, some more learning of C would be better... ;)
     
  7. thread starter macrumors newbie

    Joined:
    Aug 17, 2008
    #7
    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.
    ;)
     
  8. macrumors newbie

    Joined:
    Jul 29, 2008
    #8
    It should be:

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

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

    Cheers, Chuck
     

Share This Page