UISegmentedController - Easy - (What's Wrong?)

Discussion in 'iOS Programming' started by peacetrain67, Jul 21, 2009.

  1. peacetrain67 macrumors member

    Joined:
    Dec 20, 2007
    #1
    .m
    Code:
    - (IBAction)segaction:(id)sender
    {
    	UISegmentedControl *segmentedcontrol = (UISegmentedControl *) sender;
    	NSInteger selectedSegment = segmentedcontrol.selectedSegmentIndex;
    	
    	if (selectedSegment == 0) {
    		NSLog(@"A loaded");
    		[subview1 setHidden:NO];
    		[subview2 setHidden:YES];
    		[subview3 setHidden:YES];
    	}
    	if (selectedSegment == 1) {
    		NSLog(@"B loaded");
    		[subview1 setHidden:YES];
    		[subview2 setHidden:NO];
    		[subview3 setHidden:YES];
    	}
    	if (selectedSegment = 2) {
    		NSLog(@"C loaded");
    		[subview1 setHidden:YES];
    		[subview2 setHidden:YES];
    		[subview3 setHidden:NO];
    	}
    }
    The NSLogs are not appearing (being reached) in console. I haven't done anything with UISegmentedControls and I haven't found any good information about it. Even in sample code, it isn't pertaining to switching (adding/removing/hiding) views. I had an even harder time finding anything pertaining to how to handle this is IB. I basically scrapped this through scratch, so I expected errors, but I got none. I just didn't get any result either :( Anybody who helps - thanks for the help (PS, if the implementation is fine, I would appreciate what I should do with IB for this. I am 99.9% sure my header is good to go (how couldn't it be, haha). I did give each view it's own class, FYI). Thanks in advance.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Are you sure segaction: is getting called? (Try putting an NSLog or breakpoint right at the start to be sure.)
     
  3. peacetrain67 thread starter macrumors member

    Joined:
    Dec 20, 2007
    #3
    No, the action isn't even being called when a segment is selected (and it just shows the original - subview1 - view). I just double checked and I do have the action connected to the segmentedcontroller in IB. If it's not the code, could it be I have the XIB set up poorly in IB. (I have the XIB View [SegPractViewController] the app opens to, with three views inside of it, each with an indication of which view it is - different labels). I am obviously trying to just execute the action so I can apply it elsewhere (this is it's own project, just trying to get some example code for copy/pasting elsewhere). Thanks in advance.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    And it's connected via the "Value Changed" event? If so, what is it connected to? The view controller (File's Owner)? Is the segmentedController showing up when you start the app?
     
  5. peacetrain67 thread starter macrumors member

    Joined:
    Dec 20, 2007
    #5
    By value changed event, I assume you are reffering to segaction (from the code), and it is connected to the File's Owner. And yes, the segmentedController shows up, but doesn't respond (well, the momentary tint is the only thing that responds/changes). Am I doing something wrong in IB. Is there another method to do this (maybe some example code using subviews, and I could see what I am doing wrong?)
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    No, by Value Changed event, I mean that very event in the Connections Inspector in Interface Builder. Should look something like this:
     

    Attached Files:

  7. peacetrain67 thread starter macrumors member

    Joined:
    Dec 20, 2007
    #7
    Thanks for taking the time to get the screenshot, and I did make the change (from Touch Up Inside, I still am seeing this as more of a button then not, I guess). Even now though, I am still not having success: The segmentcontroller now does not appear, and it appears SubclassC is loaded as the application launches instead of View.

    Heres a picture of my IB Document also, if that may explain something:
     

    Attached Files:

  8. FarSeide macrumors 6502a

    FarSeide

    Joined:
    Feb 17, 2008
    Location:
    Earth Lane
    #8
    do you have your segmented control connected to your controller via IB ? It looks like your segmented control might not be calling your IBAction method. And the events for segmented control should be "Value Changed"
     
  9. peacetrain67 thread starter macrumors member

    Joined:
    Dec 20, 2007
    #9
    i think you mean "do i have my segmented control connected to my action via IB? The answer to that is yes. The segmentedcontrol is now calling the IBAction, and the events are now "Value Changed". :(Luckily, a few more problems have arisen so I can continue battling something that seems so simple. My problem now is that all 3 subviews cannot be located correctly without becoming subviews of eachother. Secondly, the application opens to Subclass C instead of Subclass A. I don't know why that is (and yes, the Subclass A segment is highlighted when the app opens). When I navigate to some other views, I can choose the segment it started on (that opens to Subclass C), and it calls the correct Subclass A. There should be simple solutions to both of these, I predict. Anybody want to inform me what they are? :D Thanks for helping me with the little kinks I had before - For some reason there aren't any decent code examples/tutorials on UISegmentedControls to switch views.
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    That's probably because Apple and most others use a Tab Bar instead. But, just so you know, my app, [app]a.k.a.[/app], actually does use a segmentedController to switch views. I just don't think I can help much more without seeing a lot more of your IB and code setup.
     
  11. peacetrain67 thread starter macrumors member

    Joined:
    Dec 20, 2007
    #11
    Well, I actually use a TabBarController in my actually application (in which I want to implement segmentedcontrollers in a variety of places. One of the habits I have tried to make as a newer developer is to be able to use every any class/api I implement in it's own application before I add it into the one I actually see value in (eventually).

    The IB Document still looks like it did in that last thumbnail I added.
    The code currently is:
    Code:
    - (IBAction)segaction:(id)sender
    {
    	NSLog(@"Action loaded");
    	UISegmentedControl *segmentedcontrol = (UISegmentedControl *) sender;
    	NSInteger selectedSegment = segmentedcontrol.selectedSegmentIndex;
    	
    	if (selectedSegment == 0) {
    		NSLog(@"A loaded");
    		[subview1 setHidden:NO];
    		[subview2 setHidden:YES];
    		[subview3 setHidden:YES];
    	}
    	else if (selectedSegment == 1) {
    		NSLog(@"B loaded");
    		[subview1 setHidden:YES];
    		[subview2 setHidden:NO];
    		[subview3 setHidden:YES];
    	}
    	else if (selectedSegment = 2) {
    		NSLog(@"C loaded");
    		[subview1 setHidden:YES];
    		[subview2 setHidden:YES];
    		[subview3 setHidden:NO];
    	}
    }
    I don't know what else to show you except Inspector, which is exactly as you'd expect it. Now that I think about it: Would I be able to add and remove subviews if I redid this with each view having it's own class/XIB? That should make loading views proportions correct, I believe. I can also just handle the wrong first view by using a viewDidLoad I believe.

    *Some tweaking in the size inspector fixed the shape/position problem (I don't even remember working with the size inspector yesterday). Now, I just need to figure out why (and how to change the fact that) SubclassC loads first instead of SubclassA (And the first segment is assigned to Subclass A). I'll put up a quick written tutorial somewhere on this, among some other basic things I've found to be a struggle in finding help with.
     
  12. peacetrain67 thread starter macrumors member

    Joined:
    Dec 20, 2007
    #12
    Adding the following solved the problem of the wrong view loading first (Obviously the NSLog isn't necessary, but that is another healthy habit I am trying to get into):
    Code:
    - (void)viewDidLoad {
        [super viewDidLoad];
    	NSLog(@"A loaded - App started");
    	[subview1 setHidden:NO];
    	[subview2 setHidden:YES];
    	[subview3 setHidden:YES];
    }
     

Share This Page