iphone - getting the button id in a segmented control - iphone

I am trying to create something that uses the idea of the UIPopOverController, I mean, have that speech bubble anchor pointing to the button who triggered the method.
The problem is that I am using a UISegmentedControl...
How do I get the id of the button that was tapped on a UISegmentedControl, so I can determine its position and generate the anchor?
thanks for any help.

Look at SegmentViewController.{h,m} in the UICatalog sample project.
The sample code sets up the view using code.
This line
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
is where the magic happens.
- (void)segmentAction:(id)sender
{
//NSLog(#"segmentAction: selected segment = %d", [sender selectedSegmentIndex]);
}
You can also do this in Interface Builder
Assuming that you have (IBAction)segmentAction:(id)sender declared in your ViewController and that the Files Owner is your ViewController, right-click on the UISegmentedControl and make the connection between the UIControlEventValueChanged event to the Files Owner segmentAction
UISegmentedControl acts like a "single" button. It is a single object placed in a single frame unlike the toolbar items which are an array of objects placed in their own frames. A typical usage pattern is to dispatch a message inside a switch statement in your segmentAction method.
I see two options.
Option A: Implement a custom view that contains an array of UIButton controls. The action for the UITouchUpInside event for the all of the buttons can point to the same method in your controller. Then implement a buttonPressed:(id)sender which you can ask the sender for it's frame and location to use to create your new view on top. --> Complicated
Option B: You can calculate the position of the new subview using an offset calculated from selectedSegmentedIndex, widthForSegmentAtIndex:, and contentOffsetForSegmentAtIndex:. Really not much different than calculating from the bounds of any view. --> Easier

Related

How to add an event that's created on one view but being displayed from a different view controller?

I have a ViewController that will be adding two subviews (UIViews) to it depending on what goes on. I want the subview to have UIButtons with events on it but when I do:
[myButton addTarget:self action:#selector(switchCategory:) forControlEvents:UIControlEventTouchDown];
on the subview, nothing responds when the button is pressed.
How do I get this working?
To be more specific, the files would be UIViewController, UIViewA, UIViewB and each UIView file will have its own events. and in the Controller I'm just doing addSubview:UIViewA/UIViewB
Found the fix for it. Being that I didn't give my view a size, it wasn't processing the events.
When dealing with UIView actions/animations, there are 2 main things that you are doing:
1: Make sure you give it a frame, or size to create the visual part.
This can be done using
CGRectMake()
as you probably already know.
2: adding the view on top of the view as a subview.
This can be done by
[self.view addSubview:viewA/viewB];

Resizing and rearranging component controls using code/ IB

Is it possible to resize and rearrange UI components (say segmented controls, buttons, labels, etc) when these are created using IB?
I need to rearrange/resize few UI components on a button click on iPad screen.
Any idea?
It is possible.
You will need to create IBOutlets for the UI components you are interested in, then you can adjust the properties you are interested in (UIView's frame property)
IBOutlet's are created in the *.h file for you View controller.
IBOutlet UIView * view;
Then you can control click and drag from the file owner (in xib) to the UI element. (or right click and drag)
Yes it is. Implement the viewDidLoad method in your controller and set the frame to whatever you wish for your views and controls so long as you've attached them to an outlet.
Example:
self.myCustomView.frame = CGRectMake(x,y,width,height);
You may also access them by their tag.
If you assign them unique tag numbers in IB, then in your code, you can search within subviews of your view and find them by viewWithTag method.
You can set the initial position of the components in IB, hook up the elements with IBOutlets and on the button click move them in code. I don't think you can rearrange them in IB.
For a cool effect you can use [UIView animateWithDuration:animations:] to move whatever components you need

UIButton to forward events & retrieve values according to UIButton

I have a for loop that displays 7 UIButton all with a method pointing to the same view ( for loop , thats why) .
What i want is that when for
e.g ,i click on button1 , the next view will display values that belongs to button1 .
and when i click on button2 , the next view (same view again) will display values that belongs to button2.
Any idea on how to work on that?
I have classes and appDelegate to store this values in it
Edit:
my UIButton is programmatically created, i cannot link IBAction to it. I think a few have mistaken about my question.
For that, When you are looping for button, at that time, give tag value to button. And create one method like below:
-(IBAction)buttonClicked:(id)sender{
int clickedTag = [sender tag];
}
inside that, you will get the Tag of button which is clicked. And you can do according to Tag.
Just set the tag while you are creating the buttons programmatically.
[button setTag:counter]; // if you are creating the buttons in For loop
Cheers.
It sounds like all these buttons probably have the same action, right? Give each button a different value for its tag property. The action can then look at the tag of the object that sent the message, e.g. sender.tag, to figure out which button was tapped. Use that information to figure out what data to display in the next view.
Simple. Set the tag value for all the buttons. Use the sender object to display the values accordingly in your view.

UITableView Header Selection

Can anyone explain me how can I detect a selection in a table view Header view like in tableView:didSelectRowAtIndexPath: ? It would be very useful for me. I've defined the header as UITableCellView so I could attach a UISwitch as an accessory view, and it worked great but now I can't detect the changes made in the switch. Any suggestions?
I'm going to assume you're dynamically creating the switches, not creating them in IB. If that's the case, you'll need to do two things to receive and distinguish changes to your switches. First, when you create the switch, set its tag property to some value that represents the corresponding section (like maybe the section index itself). Then, add an event handler to the switch that will call back to a method on your view controller.
So, in tableView:viewForHeaderInSection:, lazily instantiate your header view, maybe caching it, then say something like:
[switch setTag:section];
And add the event handler thusly, where switchToggled: is defined just as any other IBAction would be:
[switch addTarget:self
action:#selector(switchToggled:)
forControlEvents:UIControlEventValueChanged];
Then, just cast the sender you receive in your event handler to UISwitch *, and use the tag property to tell which switch was toggled.

Initialize UIPickerView

I want to create a UIPickerView with the easiest way possible, because all of what I know Is not enough to help me
Ok what I ment is that I am trying to get a UISegmented that will end up creating a UIPickerView but I want to create a UIPickerView that shows up when I select one of the segments in the segmented control animated, and also the pickerview is supposed to use an array that has colors like red green blue
Ok what I ment is that I am trying to get a UISegmented that will end up creating a UIPickerView but I want to create a UIPickerView that shows up when I select one of the segments in the segmented control animated, and also the pickerview is supposed to use an array that has colors like red green blue
For your UISegmentedControl, you'll need to register target-action methods. For example:
[segmentedControl addTarget:self
action:#selector(updatePicker:)
forControlEvents:UIControlEventValueChanged];
Inside of updatePicker, set a variable that corresponds to whatever you want to show in your picker. Then, to your picker instance (you should only have one instance of this class), send the reloadAllComponents message. This will update the picker by querying the delegate for new data. In your delegate (which can probably all be the same class), you'll want to check the value of the variable you set in updatePicker to determine what data to return to the picker.
This page has video and instructions on how to add UIPickerView programmatically in Swift, i.e. it does not use Interface Builder or Story Board, everything is done in a few lines of code.
http://www.appswiftprogramming.com/pg/2171/UIPickerView_Programmatically
You can add your custom view in the UIPickerView if you want something more advanced. It shows that too.