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

peacetrain67

macrumors member
Original poster
Dec 20, 2007
68
0
.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.
 
Are you sure segaction: is getting called? (Try putting an NSLog or breakpoint right at the start to be sure.)

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.
 
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?

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?)
 
By value changed event, I assume you are reffering to segaction (from the code), and it is connected to the File's Owner.
No, by Value Changed event, I mean that very event in the Connections Inspector in Interface Builder. Should look something like this:
 

Attachments

  • Picture 1.png
    Picture 1.png
    33.6 KB · Views: 383
No, by Value Changed event, I mean that very event in the Connections Inspector in Interface Builder. Should look something like this:

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:
 

Attachments

  • Picture 9.png
    Picture 9.png
    28.3 KB · Views: 61
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"
 
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"

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.
 
For some reason there aren't any decent code examples/tutorials on UISegmentedControls to switch views.
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.
 
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.

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.
 
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];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.