Silver challenge: How to "connect" UISegmentedControl


#1

Hello -

I’m working on the silver challenge and I’ve gotten stuck at connecting the UISegmentedControl to my code. Here are the the things I’ve tried:

  1. I wanted to right-click on it and then drag the delegate circle to file’s owner. Unfortunately, for reasons I don’t understand, there is no delegate option there – screenshot – just a list of events.

  2. So I scoured the forums and I saw a couple people who noted that you could wire the UISegmentedControl to the file’s owner by doing a Ctrl-drag. I tried this, but it also didn’t work: I could drag to the first responder (which would get highlighted in blue before I released the drag), but when I dragged to the file’s owner nothing happened.

  3. I’ve also looked at the documentation, which clearly states that you can use the UIControlEventValueChanged constant. I’m not sure where to put that constant for it to be checked by anything.

  4. Another poster on this forum created a method called changedMapType that was added as a target, so I thought I’d try that. When I right-click on the UISegmentedControl, I see a nice list of events including Value Changed. So I tried to drag the little circle next to Value Changed to the file’s owner. But I got the same results as in attempt #2 above – nothing happened. I couldn’t drag to the file’s owner.

So what am I missing? Should I be connecting it to something besides the file’s owner? I wanted to model the UISegmentedControl setup like the UITextField’s setup, since they seemed so similar (both taking input from the user, both affecting the view in some way). Do I have some fundamental misunderstanding?

Thanks very much! I’ve been working on this over the weekend and I would be thrilled to know what’s going on.


#2

Hi, well I did something like this. I had the same problem like you at first.
First implement this method:
[- (IBAction)changeMap:(UISegmentedControl *)set
{

if (set.selectedSegmentIndex==0) {
    worldView.mapType=MKMapTypeStandard;
}else if (set.selectedSegmentIndex==1) {
    worldView.mapType=MKMapTypeSatellite;
}else if (set.selectedSegmentIndex==2) {
    worldView.mapType=MKMapTypeHybrid;
}

}]

And for connecting just click and drag from UI segment controller that you have put on your interface to the change map in file owners, thats all.


#3

I also found that UISegmentedControl didn’t seem to allow for delegation, so I:

[ol]
[li] Created an IBOutlet instance variable for it in WhereamiViewController.h[/li]
[li] Connected it in Interface Builder by control-dragging from File’s Owner to the control[/li]
[li] Defined a method in WhereamiViewController.m to receive an event message when the user touched the control[/li]
[li] Invoked addTarget:action:forControlEvents: on that instance variable in viewDidLoad in WhereamiViewController.m[/li][/ol]


#4

Can you post your code, and tell how you figured out how to do this? I couldn’t, from looking at the API reference for UISegmentControl and its parent UIView. Thanks.


#5

macintux’s instructions are as clear as arctic ice crystals. If you are having difficulty, you should go back and review such basic concepts as outlets, actions, file’s owner, etc.

Also UISegmentedControl Class Reference shows you how to register the target-action methods: “You register the target-action methods for a segmented control using the UIControlEventValueChanged constant as shown below.”

You should also check out the samples in UISegmentedControl Class Reference’s Related sample code section.


#6

Are there other ways of registering target-action methods? I see from previous posts that I can insert the code:

That works well for me, but I found that instead of using that code, I can simply go to the storyboard, drag an arrow from the segmented control to the Whereami View Controller icon, and connect the arrow to the proper IBAction.

Are these just two equivalent means to achieve the same ends? Is one way better or more accepted than the other?


#7

Thanks for the post!